开发者

Find and hide div (jQuery)

<a rel="abc" href="#mydiv">link</a>
<div id="mydiv">content</div>

开发者_StackOverflowIf rel="abc", find element with ID that matches href value and hide it.

My try:

$('[rel*=abc]').attr("href").hide();

Thanks for your help!


$( $("a[rel='abc']").attr("href") ).hide();


$("a[rel='abc']").click(function(event){
  event.preventDefault();
  var val = $(this).attr("href");
  $("div"+val).hide();
});


$('a[rel=abc]').click( function(event){
    event.preventDefault();
    $(event.target.href.substr(event.href.indexOf('#'))).hide();
});

Hides the appropriate element if such a link is clicked.

edit: tested


$('a[rel=abc]').each(function() { $(this.href.substr(this.href.indexOf('#'))).hide(); });

Some error checking would be good too.


$("a[rel=abc]").each(function(i, ele) {
    $(ele.hash).hide();
});

or if you want that to happen on click

$("a[rel=abc]").click(function(e) {
    e.preventDefault();
    $(this.hash).hide();
});


var identifier = $('a[rel="abc"]').attr('href');
$('#'+identifier').hide();

I think this may solve your problem. I didn't test it, though.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜