Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果元素具有id,则在页面上写入每个元素的id_Javascript_Jquery - Fatal编程技术网

Javascript 如果元素具有id,则在页面上写入每个元素的id

Javascript 如果元素具有id,则在页面上写入每个元素的id,javascript,jquery,Javascript,Jquery,需要在页面上写入每个元素的id——如果元素具有id 在纯javascript或jquery中-无所谓 这是我的尝试-没有成功 请帮忙 再来一次 document.querySelectorAll('*').forEach(function(el) { if(el.attr('id')){console.log(el.attr('id'));} }); 你能试试这个吗 document.querySelectorAll('*').forEach(function(el) { if

需要在页面上写入每个元素的
id
——如果元素具有
id

在纯javascript或jquery中-无所谓
这是我的尝试-没有成功
请帮忙

再来一次

document.querySelectorAll('*').forEach(function(el) {
    if(el.attr('id')){console.log(el.attr('id'));}
});

你能试试这个吗

document.querySelectorAll('*').forEach(function(el) {
    if(el.attributes.id){console.log(el.attributes.id);}
});

你能试试这个吗

document.querySelectorAll('*').forEach(function(el) {
    if(el.attributes.id){console.log(el.attributes.id);}
});
试试这个:

const elements=document.querySelectorAll('*'))
常量elementsWithId=Array.from(elements).filter(element=>element.id!='')
elementsWithId.forEach(element=>console.log(element.id))

没有身份证

有id

试试这个:

const elements=document.querySelectorAll('*'))
常量elementsWithId=Array.from(elements).filter(element=>element.id!='')
elementsWithId.forEach(element=>console.log(element.id))

没有身份证

有id


检查您的控制台。上面说什么?提示:jQuery的
回调的第一个参数。每个
都是索引。对jquery方法使用
this.id
。对于本机JS,只需使用
el.id
@Terry-
这个.id
-就行了检查控制台。上面说什么?提示:jQuery的
回调的第一个参数。每个
都是索引。对jquery方法使用
this.id
。对于本机JS,只需使用
el.id
@Terry-
这个.id
-就行了