84669 人が学習中
152542 人が学習中
20005 人が学習中
5487 人が学習中
7821 人が学習中
359900 人が学習中
3350 人が学習中
180660 人が学習中
48569 人が学習中
18603 人が学習中
40936 人が学習中
1549 人が学習中
1183 人が学習中
32909 人が学習中
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();这样加一个字母