我有这个 html 行:
<div class="hide-btn top tip-s" original-title="Close sidebar"></div>
Run Code Online (Sandbox Code Playgroud)
当用户点击它时,我希望文本更改为“打开侧边栏”,我该怎么做?
这是我的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");});
}
});
Run Code Online (Sandbox Code Playgroud)
感谢您的时间!
要使用 jQuery 更改元素上的属性,请使用attr. 在与 jQuery 挂钩的事件处理程序中,原始 DOM 元素可作为this. 要围绕它获取 jQuery 包装器,请使用$(this). 例如:
$(this).attr("original-title", "new text");
Run Code Online (Sandbox Code Playgroud)
旁注:该属性original-title对div元素无效。考虑改用data-*属性。