zoukankan      html  css  js  c++  java
  • 新增加的html里的标签元素,该元素作为事件,js获取不到id的问题

     if(flag==0){
                    var p = document.createElement("p");
                     var text_p = document.createTextNode("还没有账号0?");
                     p.setAttribute("class","p-rj");
                     p.setAttribute("id","prj");
                     p.appendChild(text_p);
                       var a = document.createElement("a");
                       var text_a = document.createTextNode("注册0");
                         a.setAttribute("href","javascript:void(0);");  
                         a.setAttribute("class","drop_col");
                         a.setAttribute("id","register");
                         a.setAttribute("onclick","rjSecond()");
                         a.appendChild(text_a);
                      p.appendChild(a);
                        $("boss").appendChild(p); }//改了增加HTML内容之后。发现还是没法获得id,我就设置了属性onclick最后bug解决
                    flag=1; 

    .....

                           /* $("register").onclick=function(){ 之前用的这个方式,但是没办法获id的值register  ,不知道为什么新增加的HTML里的,已经形成页面,但是再次点击这个绑定事件的标签时,id没法获取到。*/
      function rjSecond()
          { var table = document.getElementsByTagName("table")[0];
            var form = table.parentNode;
            form.removeChild(table)
            if(flag!=0){
              var div = $("submit-div");
             form.removeChild(div);
            ........
            =================

    后来终于明白了如何成功获取id了:要把所有代码都放在那个大事件的代码里:例如:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>shijian</title>
    </head>
    <body>
    <div id="box">
        <button id="myBtn">试一试</button>
    </div>
    <p id="demo"></p>
    </body>
    <script>
        document.getElementById("myBtn").onclick=function(){
      document.getElementById("box").innerHTML+="<p id='hh' >函数。</p>";

      document.getElementById("demo").innerHTML=Date();
      document.getElementById("hh").onclick=function () {
            alert("gg");
        };
    };
     

    </script>
    </html>
    与其战胜敌人一万次,不如战胜自己一次。
  • 相关阅读:
    XPath中的text()和string()区别(转)
    (转)Ubuntu 16.04 安裝Docker(PS:本文适用amd64位的ubuntu系统)
    python 爬取世纪佳缘,经过js渲染过的网页的爬取
    Python中的join()函数的用法
    Ubuntu下修改ubuntu源,完成Redis Desktop Manager的安装
    Ubuntu16.04安装Redis
    Scrapy爬虫实例教程(二)---数据存入MySQL
    Ubuntu16.04安装mongodb 及使用
    关闭和启动网卡
    网络之端口的作用
  • 原文地址:https://www.cnblogs.com/hyjh/p/11258629.html
Copyright © 2011-2022 走看看