如何从div的id开始获取内联样式(style-attribute)的所有元素?

问题描述 投票:-1回答:3

我希望得到所有具有内联风格的元素,并在div和他所有的孩子中搜索它。例

<div id="idofstartdiv">
<span style="color:red;"></span>
<div>
<div style="color:green"><span style="color:yellow;"></span><span></span></div>
</div>
</div>

我知道我可以通过$("[style]")得到所有的内联风格,我知道选择$("#idofstartdiv")的起始div。

我怎样才能将这两个选择器结合起来?

非常感谢。

javascript jquery
3个回答
4
投票

你可以简单地做到:

$("#idofstartdiv[style]")

这将获得id为idofstartdiv的元素以及style属性。要使所有具有style属性的id的父级作为父级,您可以执行以下操作:

$("#idofstartdiv").find("[style]")

如其他答案中所述,您还可以:

$("#idofstartdiv [style]")

3
投票
$("#idofstartdiv [style]") is this is what you are asking for? if not can you provide an example of what you are expecting, am not clear with your question.

1
投票

另一种选择 -

$("[style]",  "#idofstartdiv")

这也将返回父div-id“idofstartdiv”下具有style属性的所有元素。

© www.soinside.com 2019 - 2024. All rights reserved.