2012-04-07 51 views
0

我有这样的HTML行:jQuery在点击时更改原始标题文本?

<div class="hide-btn top tip-s" original-title="Close sidebar"></div> 

而当用户点击它,我想,该文本更改为“打开侧边栏”我怎么能做到这一点?

这是我的JS:

$(".hide-btn").click(function(){ 
    if($("#left").css("width") == "0px"){ 
     $("#left").animate({width:"230px"}, 500); 
     $("#right").animate({marginLeft:"250px"}, 500); 
     $("#wrapper, #container").animate({backgroundPosition:"0 0"}, 500); 
     $(".hide-btn.top, .hide-btn.center, .hide-btn.bottom").animate({left: "223px"}, 500, function() { $(window).trigger("resize");}); 
    } 
    else{ 
     $("#left").animate({width:"0px"}, 500); 
     $("#right").animate({marginLeft:"20px"}, 500); 
     $("#wrapper, #container").animate({backgroundPosition:"-230px 0px"}, 500); 
     $(".hide-btn.top, .hide-btn.center, .hide-btn.bottom").animate({left: "-7px"}, 500, function() { $(window).trigger("resize");}); 
    } 
}); 

谢谢您的时间!

+0

[如何使用jQuery更改元素的标题属性](http://stackoverflow.com/questions/987967/how-to-change-an-elements-title-attribute-using-jquery)可能的重复..这会帮助你。 – 2012-04-07 09:54:33

回答

2

要使用jQuery更改元素上的属性,请使用attr。在与jQuery相连的事件处理程序中,原始DOM元素可用作this。要获得一个jQuery包装器,请使用$(this)。例如: -

$(this).attr("original-title", "new text"); 

边注:该属性original-titlediv元素无效。考虑使用data-* attributes来代替。

1

我喜欢做这一块的那种事:-)

var a = $(".hide-btn.top"); 
a.attr('original-title', a.attr('original-title').replace(/Close/, 'Open')); 

看来可能过于复杂,但我经常改变我的主意之后的确切标签,这样节省了我要回去和调整一切。