最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery - If DIV is present on page then add CSS to other DIV - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

4 Answers 4

Reset to default 9
if ($('#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>
发布评论

评论列表(0)

  1. 暂无评论