jquery访问所有具有id属性的元素(jquery accessing all elements who have id attribute)

我想从一个div中拥有id attribute所有元素,意味着我想要执行如下操作:

$("div").find("*[id]").each(function() { alert(this.id) });

但这不起作用,有人可以帮我吗?

I want to get all elements from a div who have id attribute, means i want to do something like:

$("div").find("*[id]").each(function() { alert(this.id) });

but this is not working , can anybody help me pls?

最满意答案

你的代码工作得很好,但你可以从选择器中删除* 。

其他有效选项:

$("div").find("[id]").each(function() { alert(this.id) });

现场演示

或这个:

$("div *").filter("[id]").each(function() { alert(this.id) });

现场演示

或这个:

$("div [id]").each(function() { alert(this.id) }); // which I think is the best

现场演示

Your code works just fine, but you can remove the * from the selector.

Other valid options:

$("div").find("[id]").each(function() { alert(this.id) });

LIVE DEMO

Or this:

$("div *").filter("[id]").each(function() { alert(this.id) });

Live DEMO

Or this:

$("div [id]").each(function() { alert(this.id) }); // which I think is the best

Live DEMO

jquery访问所有具有id属性的元素(jquery accessing all elements who have id attribute)

我想从一个div中拥有id attribute所有元素,意味着我想要执行如下操作:

$("div").find("*[id]").each(function() { alert(this.id) });

但这不起作用,有人可以帮我吗?

I want to get all elements from a div who have id attribute, means i want to do something like:

$("div").find("*[id]").each(function() { alert(this.id) });

but this is not working , can anybody help me pls?

最满意答案

你的代码工作得很好,但你可以从选择器中删除* 。

其他有效选项:

$("div").find("[id]").each(function() { alert(this.id) });

现场演示

或这个:

$("div *").filter("[id]").each(function() { alert(this.id) });

现场演示

或这个:

$("div [id]").each(function() { alert(this.id) }); // which I think is the best

现场演示

Your code works just fine, but you can remove the * from the selector.

Other valid options:

$("div").find("[id]").each(function() { alert(this.id) });

LIVE DEMO

Or this:

$("div *").filter("[id]").each(function() { alert(this.id) });

Live DEMO

Or this:

$("div [id]").each(function() { alert(this.id) }); // which I think is the best

Live DEMO