试图通过标记名或id添加innerhtml,但它没有显示效果

时间:2017-12-31 09:10:58

标签: javascript html

我正在尝试添加名为P.I的元素的innerHtml。我添加了其属性id,其值为p1。我曾尝试在点击按钮时更改innerhtml。但总的来说,我没有这样做。我仍然不知道为什么不这样做。

<!DOCTYPE html>
<html>
<head>
    <title>javascript </title>
</head>
<body>
<button onclick="myfunction()">click me</button>
<p id="new"> this my content added by manully</p>
<p id="p1"></p>

<script type="text/javascript">

    document.getElementById("new").innerHtml="this is my first page";
    function myfunction(){
        //alert(1);
        document.getElementById("p1").innerHtml="this is my first page";
        //console.log();
    }

</script>
</body>
</html>

我正在关注这个成功显示结果的例子。请关注它。

<!DOCTYPE html>
<html>
<body>

<p>Click the button to trigger a function that will output "Hello World" in a p element with id="demo".</p>

<button onclick="myFunction()">Click me</button>

<p id="demo"></p>

<script>
function myFunction() {
    document.getElementById("demo").innerHTML = "Hello World";
}
</script>

</body>
</html>

2 个答案:

答案 0 :(得分:1)

我使用了错误的javascript函数语法,它被命名为innerHTML,但我将它用作innerHtml。

<!DOCTYPE html>
<html>
<head>
    <title>javascript </title>
</head>
<body>
<button onclick="myfunction()">click me</button>
<p id="new"> this my content added by manully</p>
<p id="p1"></p>

<script type="text/javascript">

    document.getElementById("new").innerHTML="this is my first page";
    function myfunction(){
        //alert(1);
        document.getElementById("p1").innerHTML="this is my first page";
        //console.log();


    }


</script>
</body>
</html>

答案 1 :(得分:0)

试试这个:

<script>
        $(document).on('click', '#new', function () {

            $(this).html('my msg');

        })
    </script>