定位没有类或id的特定元素

时间:2015-08-11 09:27:26

标签: javascript jquery html dom siblings

我有一个动态的HTML,我想在第二个FormItem div中定位textarea(我不想用CSS做这个)。但是这个textarea没有特定的类或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");

但它还没有奏效,我做错了什么?

And my jsfiddle

3 个答案:

答案 0 :(得分:1)

尝试将:not()选择器与attribute selector混合以获取其中没有idclass属性的元素,

$("h2#Panel").next("fieldset").find(".FormItem:nth-child(2) > textarea:not([id],[class])")

DEMO

答案 1 :(得分:1)

在您的上下文中的jquery中使用apply(cbind(data,1:nrow(data),1,function(x){ apply(list_ngrams,1,function(y){ if(grepl(y[2],x[1])==TRUE){idv_x_bags[x[2],str_trim(as.character(y[1]))]<-1} }) })

.find()

Fiddle

答案 2 :(得分:0)

这可能有效 -

$("h2#Panel").next().find("textarea").css("background-color", "red");

JSFiddle

相关问题