定位没有 class 或 id 的特定元素
Target a specific element with no class or id
我有一个 HTML 的动态片段,我想在第二个 FormItem div 中定位一个文本区域(我不想用 CSS 做这个)。但是这个 textarea 没有特定的 class 或 id。 HTML如下:
<h2 id="Panel" class=""><a href="#" >SEO</a></h2>
<fieldset>
<div class="FormItem">
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
</div>
<div class="FormItem">
<p>Some text</p>
<textarea rows="2" cols="20" style="height: 40px;"></textarea>
</div>
<div class="FormItem">
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
</div>
</fieldset>
我的 jQuery 代码是:
$("h2#Panel").next("fieldset .FormItem:nth-child(2) textarea").css("background-color", "red");
但是还是不行,我哪里做错了?
尝试将 :not()
选择器与 attribute selector
混合使用,以获取其中没有 id
或 class
属性的元素,
$("h2#Panel").next("fieldset").find(".FormItem:nth-child(2) > textarea:not([id],[class])")
DEMO
在您的上下文中使用 jquery 中的 .find()
$("h2#Panel").next("fieldset").find('.FormItem:nth-child(2) textarea').css("background-color", "red");
这可能有效 -
$("h2#Panel").next().find("textarea").css("background-color", "red");
我有一个 HTML 的动态片段,我想在第二个 FormItem div 中定位一个文本区域(我不想用 CSS 做这个)。但是这个 textarea 没有特定的 class 或 id。 HTML如下:
<h2 id="Panel" class=""><a href="#" >SEO</a></h2>
<fieldset>
<div class="FormItem">
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
</div>
<div class="FormItem">
<p>Some text</p>
<textarea rows="2" cols="20" style="height: 40px;"></textarea>
</div>
<div class="FormItem">
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
</div>
</fieldset>
我的 jQuery 代码是:
$("h2#Panel").next("fieldset .FormItem:nth-child(2) textarea").css("background-color", "red");
但是还是不行,我哪里做错了?
尝试将 :not()
选择器与 attribute selector
混合使用,以获取其中没有 id
或 class
属性的元素,
$("h2#Panel").next("fieldset").find(".FormItem:nth-child(2) > textarea:not([id],[class])")
DEMO
在您的上下文中使用 jquery 中的 .find()
$("h2#Panel").next("fieldset").find('.FormItem:nth-child(2) textarea').css("background-color", "red");
这可能有效 -
$("h2#Panel").next().find("textarea").css("background-color", "red");