随笔-167  评论-65  文章-0  trackbacks-0

在做rails的无刷新的数据更新时,老是在replace 和 replace_html上出问题,当你遇到此问题时,不烦二者尝试下看看,看了下rails API

replace(id, *options_for_render)

Replaces the "outer HTML" (i.e., the entire element, not just its contents) of the DOM element with the given id.

options_for_render may be either a string of HTML to insert, or a hash of options to be passed to ActionView::Base#render. For example:
  # Replace the DOM element having ID 'person-45' with the
  # 'person' partial for the appropriate object.
  page.replace 'person-45', :partial => 'person', :object => @person

This allows the same partial that is used for the insert_html to be also used for the input to replace without resorting to the use of wrapper elements.

Examples:
  <div id="people">
    <%= render :partial => 'person', :collection => @people %>
  </div>

  # Insert a new person
  #
  # Generates: new Insertion.Bottom({object: "Matz", partial: "person"}, "");
  page.insert_html :bottom, :partial => 'person', :object => @person

  # Replace an existing person

  # Generates: Element.replace("person_45", "-- Contents of partial --");
  page.replace 'person_45', :partial => 'person', :object => @person

Source: show | on GitHub
replace_html(id, *options_for_render)

Replaces the inner HTML of the DOM element with the given id.

options_for_render may be either a string of HTML to insert, or a hash of options to be passed to ActionView::Base#render. For example:
  # Replace the HTML of the DOM element having ID 'person-45' with the
  # 'person' partial for the appropriate object.
  # Generates:  Element.update("person-45", "-- Contents of 'person' partial --");
  page.replace_html 'person-45', :partial => 'person', :object => @person

Source: show | on GitHub

从api中可以看出replace相当于  OuterHtml , replace_html相当于 InnerHtml,那么OuterHtml 和 InnerHtml有什么区别呢,大致解释如下:

innerHTML 设置或获取位于对象起始和结束标签内的 HTML
outerHTML 设置或获取对象及其内容的 HTML 形式
innerText 设置或获取位于对象起始和结束标签内的文本
outerText 设置(包括标签)或获取(不包括标签)对象的文本

innerText和outerText在获取时是相同效果,但在设置时,innerText仅设置标签内的文本,而outerText设置包括标签在内的文本。

ref:

http://www.cnblogs.com/zpq521/archive/2008/07/12/1241493.html

http://blog.csdn.net/lynnlin1122/archive/2008/09/09/2905442.aspx

http://www.87cool.com/articleShow.aspx?number=52

write by feng
posted on 2009-04-15 18:44 fl1429 阅读(805) 评论(0)  编辑  收藏 所属分类: Rails

只有注册用户登录后才能发表评论。


网站导航:
 
已访问数:
free counters