开发者

Jquery find if the page contains specific id?

Is there some one can help me about this? If the page contains i开发者_Go百科d = "item1" executes #home.hide(); I am really frustrated about this. My code:

<tr>
<td id = "item1">
</tr>

if($("body:has(#item1)")){
$('#home').hide();
}


If what you're trying to do is to execute $('#home').hide(); only if the #item1 object is present, then you would do that like this:

if ($("#item1").length > 0) {
    $('#home').hide();
}

There is no need for checking if #item1 is in body since that's the only place it can be. You can simply just check for #item1 since ids must be unique.

You could even resort to plain JS for the condition as an illustration of how simple it is:

if (document.getElementById("item1")) {
    $('#home').hide();
}

If that isn't what you're trying to do, then please clarify your question further.


All you need is:

<script type="text/javascript">
$(function() {
    if($("#item1").length) {
        $('#home').hide();
    }
});
</script>


u can check like this

if($('#item1').length){
    $('#home').hide();
}

this will return true if there is such an element as'item1'


if($('#item1').length) $('#home').hide();

There are other ways, but that's the simplest.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜