84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
var id=Math.random(); //var id="mm" $('body').html('<p id='+id+'>hha</p>'); $('#'+id).removeAttr('id');
因为生成的id中包含小数点,这个刚好与class选择器冲突了,不建议在id中包含小数点,这个需要这样改才能选择到这个元素
$(('#'+id).replace('.', '\\.')).removeAttr('id');
数字是不允许作为选择器使用的,你去控制台看报错
还有就是你去控制台
document.querySelector('#1') // Uncaught DOMException: Failed to execute 'querySelector' on 'Document': '#1' is not a valid selector.
可以使用原生的另一个方法试下,
document.getElementById('#1') //null
你改写成 getElementById 试下
getElementById
不要使用数字开头的 id 属性!在某些浏览器中可能出问题。console.log($('#'+id));//[] 没找到元素
嗯嗯,但是实际上追加到DOM上面的是字符型数字
数字是不可以用作id的,你可以这样:var id = =''+d+Math.random();这样加一个字母
生成随机字符串: randomString(6); function randomString(len) { len = len || 32; var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';//可以自定义 var maxPos = $chars.length; var pwd = ''; for(i = 0; i < len; i++) { pwd += $chars.charAt(Math.floor(Math.random() * maxPos)); } return pwd; }
因为生成的id中包含小数点,这个刚好与class选择器冲突了,不建议在id中包含小数点,这个需要这样改才能选择到这个元素
数字是不允许作为选择器使用的,你去控制台看报错
还有就是你去控制台
可以使用原生的另一个方法试下,
你改写成
getElementById
试下不要使用数字开头的 id 属性!在某些浏览器中可能出问题。
console.log($('#'+id));//[] 没找到元素
嗯嗯,但是实际上追加到DOM上面的是字符型数字
数字是不可以用作id的,你可以这样:var id = =''+d+Math.random();这样加一个字母