If DIV is present on page then add CSS to other DIV

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 :)


if ($('#dynamicChat').length > 0) {
    $('.prdSection').css('height', '25px');
}

你不需要“> 0”,你可以简单地写:

if ( $('#myElement').length ) {
// it exists
}

if ( $('#dynamicChat').length ) {
$('.prdSection').css('height', '25px');
}

不需要jQuery来检查该元素是否存在:

if(document.getElementById('dynamicChat')){
    $('.prdSection').css('height', '25px');
}
链接地址: http://www.djcxy.com/p/83702.html

上一篇: jquery将div添加到新div

下一篇: 如果页面上存在DIV,则将CSS添加到其他DIV