JQuery changing content of table cell

南笙酒味 提交于 2019-12-23 09:51:44

问题


OK, this is an embarrassingly simple question. Why doesn't the following jQuery example work? Obviously it is supposed to change the 'a' in the table to 'hello'.

HTML code:

    <table id='table1'>
      <tr>
          <td>a</td>
          <td>b</td>
      </tr>
    </table>​

JavaScript (JQuery) code:

    $("#table1 td:contains('a')").innerHTML="hello";

回答1:


use the html function like this

 $("#table1 td:contains('a')").html("hallo");

if you want use innerHTML (is a DOM method, not a Jquery Method), you have to select the DOMElement first.

jQuery(document).ready(function(){
    $("#table1 td:contains('a')").each(function(){
    jQuery(this)[0].innerHTML = "Hallo";
    });
});



回答2:


It doesn't work because innertHTML is a property of a DOM element and not of the jQuery object. You want

$("#table1 td:contains('a')").html("hello");  


来源:https://stackoverflow.com/questions/9411459/jquery-changing-content-of-table-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!