如何用jquery更改内部html

时间:2014-06-24 07:33:49

标签: javascript jquery html

我有一个包含2个div的html文件,

当jquery运行div d2的内部html时

应改为" m" ,由于某种原因代码不起作用

<!DOCTYPE html>
<html>
    <head>
        <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    </head>
    <body>
        <p>This example calls a function which performs a calculation, and returns the result:</p>
        <p id="demo"></p>
        <p id="d2"></p>
        <script>
            function myFunction(a, b) {
                return a * b;
            }
            document.getElementById("demo").innerHTML = myFunction(4, 3);

            $( document ).ready(function() {
                $("d2").html("m"); 
            });
        </script>
    </body>
</html>

2 个答案:

答案 0 :(得分:3)

替换

$("d2").html("m"); 

$("#d2").html("m"); 

ID Selector (“#id”)
Selects a single element with the given id attribute.

答案 1 :(得分:2)

jQuery中使用#代表id。将$('d2')更改为$("#d2").

所以你的脚本就像这样

$( document ).ready(function() {
      $("#d2").html("m");    
 });