[英]jQuery to add a class to another div
I have the following code where if a Div called 'VALID_VAR_264' contains the word 'Required' then the class 'sponz' is added to the div. 我有以下代码,如果如果名为“ VALID_VAR_264”的Div包含单词“ Required”,则将“ sponz”类添加到div。
However, how can I adapt this so that if the Div called 'VALID_VAR_264' contains 'Required' then I can get it to add the class 'sponz' to a different div. 但是,我该如何调整它,以便如果名为“ VALID_VAR_264”的Div包含“ Required”,那么我可以将其添加为将“ sponz”类添加到其他div。
Is this possible? 这可能吗?
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">//<![CDATA[
$(window).load(function(){
jQuery('#VALID_VAR_264:contains("Required")').addClass('sponz')
});//]]>
</script>
You can get the length of the jQuery object and if it is greater than 0 execute the code you need to: 您可以获取jQuery对象的长度,如果长度大于0,请执行以下代码:
if(jQuery('#VALID_VAR_264:contains("Required")').length>0)
{
$(otherSelector).addClass('sponz');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.