admin管理员组文章数量:1316523
I'm very new to jQuery BUT I've got the basics down... At least I think I do? I'm just not sure how to put them together for them to actually work.
Example:
IF (div with ID of #dynamicChat) is present on the page. Then (div with class of .prdSection) height is 25px. Else do nothing
What I have so far:
$('#dynamicChat'){
$('.prdSection').css('height', '25px');
}
I don't think I'm far off... Saying that I probably am :) Any help would be great!
*Side question - If I'm referring to a Div on a page, what would I call it? An Element?
Thanks :)
I'm very new to jQuery BUT I've got the basics down... At least I think I do? I'm just not sure how to put them together for them to actually work.
Example:
IF (div with ID of #dynamicChat) is present on the page. Then (div with class of .prdSection) height is 25px. Else do nothing
What I have so far:
$('#dynamicChat'){
$('.prdSection').css('height', '25px');
}
I don't think I'm far off... Saying that I probably am :) Any help would be great!
*Side question - If I'm referring to a Div on a page, what would I call it? An Element?
Thanks :)
Share Improve this question asked Mar 14, 2014 at 19:46 NickNick 2,5595 gold badges39 silver badges72 bronze badges 3- you can always check the .length of a jq query to see if it is >0 – jumpdart Commented Mar 14, 2014 at 19:48
- possible duplicate of Check if element exists in jQuery – aksu Commented Mar 14, 2014 at 19:55
- Not sure how you would consider this a duplicate – Nick Commented Mar 14, 2014 at 19:59
4 Answers
Reset to default 9if ($('#dynamicChat').length > 0) {
$('.prdSection').css('height', '25px');
}
You don’t need the “> 0”, you can simply write:
if ( $('#myElement').length ) {
// it exists
}
if ( $('#dynamicChat').length ) {
$('.prdSection').css('height', '25px');
}
No need of jQuery to check if that element exist :
if(document.getElementById('dynamicChat')){
$('.prdSection').css('height', '25px');
}
If (div with ID of #dynamicChat) is present on the page. Then (div with class of .prdSection) height is 25px, else do nothing
<script type="text/javascript">
if ($('#dynamicChat').is("visible")) {
$('.prdSection').css('height', '25px');
}
</script>
本文标签: javascriptjQueryIf DIV is present on page then add CSS to other DIVStack Overflow
版权声明:本文标题:javascript - jQuery - If DIV is present on page then add CSS to other DIV - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742000929a2410997.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论