代码之家  ›  专栏  ›  技术社区  ›  JonoW

Firefox(使用.innerHTML)中的jQuery html()忽略DOM更改

  •  21
  • JonoW  · 技术社区  · 15 年前

    我真的很惊讶我以前没有遇到过这个问题,但是对元素调用jQueries.html()函数似乎忽略了DOM中的更改,即它返回原始源中的html。我不这样做。html()只在内部使用innerHTML属性。

    这是注定要发生的吗?我使用的是Firefox 3.5.2。下面是一个示例,无论您将textbox值更改为什么,“container”元素的innerHTML都只返回HTML标记中定义的值。示例使用jQuery并不仅仅是为了简化它(使用jQuery的结果是一样的)。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
        <head>
            <script type="text/javascript">
                <!--
                function BodyLoad(){                
                    document.getElementById("textbox").value = "initial UPDATE";
                    DisplayTextBoxValue();
                }
    
                function DisplayTextBoxValue(){
                    alert(document.getElementById("container").innerHTML);             
                    return false;
                }
                //-->
            </script>
        </head>
        <body onload="BodyLoad();">
            <div id="container">
                <input type="text" id="textbox" value="initial" />
            </div>
            <input type="button" id="button" value="Test me" onclick="return DisplayTextBoxValue();" />
        </body>
    </html>
    
    6 回复  |  直到 15 年前
        1
  •  37
  •   gnarf    12 年前

    Firefox不会更新 value 属性 基于用户输入的DOM对象的 价值 财产 -存在相当快的解决方法。

    function DisplayTextBoxValue(){
      var element = document.getElementById('textbox');
      // set the attribute on the DOM Element by hand - will update the innerHTML
      element.setAttribute('value', element.value);
      alert(document.getElementById("container").innerHTML);             
      return false;
    }
    

    jQuery插件,使 .formhtml() 自动执行以下操作:

    (function($) {
      var oldHTML = $.fn.html;
    
      $.fn.formhtml = function() {
        if (arguments.length) return oldHTML.apply(this,arguments);
        $("input,button", this).each(function() {
          this.setAttribute('value',this.value);
        });
        $("textarea", this).each(function() {
          // updated - thanks Raja & Dr. Fred!
          $(this).text(this.value);
        });
        $("input:radio,input:checkbox", this).each(function() {
          // im not really even sure you need to do this for "checked"
          // but what the heck, better safe than sorry
          if (this.checked) this.setAttribute('checked', 'checked');
          else this.removeAttribute('checked');
        });
        $("option", this).each(function() {
          // also not sure, but, better safe...
          if (this.selected) this.setAttribute('selected', 'selected');
          else this.removeAttribute('selected');
        });
        return oldHTML.apply(this);
      };
    
      //optional to override real .html() if you want
      // $.fn.html = $.fn.formhtml;
    })(jQuery);
    
        2
  •  6
  •   Philippe Leybaert    15 年前

    这是Firefox中已知的“问题”。innerHTML的规范并不完全清楚,因此不同的浏览器供应商以不同的方式实现它。

    有关此主题的讨论可在此处找到:

    http://forums.mozillazine.org/viewtopic.php?t=317838#1744233

        3
  •  2
  •   Don Santiago Elvira Ramirez    12 年前

    谢谢你的formhtml插件。 textarea ,我必须更新它:

    $("textarea", this).each(function() { 
      $(this).text($(this).val()); 
    });
    
        4
  •  1
  •   Russ Cam    15 年前

    我知道你的问题与 innerHTML ,但如果它只是 textbox 在…内 container 那你需要什么

    $('#textbox').val()
    

        5
  •  0
  •   Swaroop    13 年前

    defaultValue

    function DisplayTextBoxValue(){
      var element = document.getElementById('textbox');
      element.defaultValue = element.value;    
      alert(document.getElementById("container").innerHTML);             
      return false;
    }
    

    应该打印更新的innerHTML。

    onChange

        6
  •  0
  •   Madalin    11 年前

    此更新功能仅适用于未手动更改的输入字段。

      // set text input value from the associated sel dropdown and reset dropdown
      function set_input_value(eid, sel) {
          var el = document.getElementById(eid);
          el.setAttribute("value", sel.options[sel.selectedIndex].text);
    
          // added this line to solve the issue:
          el.value = el.getAttribute('value');
    
          sel.selectedIndex=0;
      }