2

我有这个 html 行:

<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");});
    }
});

感谢您的时间!

4

2 回答 2

7

要使用 jQuery 更改元素的属性,请使用attr. 在与 jQuery 挂钩的事件处理程序中,原始 DOM 元素以this. 要获得一个 jQuery 包装器,请使用$(this). 例如:

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

旁注:该属性original-titlediv元素无效。考虑改用data-*属性

于 2012-04-07T09:53:07.870 回答
2

我喜欢为那种事情做这个:-)

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

可能看起来过于复杂,但我经常在之后改变对确切标签的想法,这样我就不必回去调整一切。

于 2012-04-07T09:55:41.587 回答