如果按钮文本是“跟随”和用户悬停在它之上,我希望文本更改为“取消跟随”,并改变回“跟随”一旦用户停止悬停。
按钮:
{%if follow%}
<button type="submit" id="status" class="button" value="True"><span>followed</span></button>
{%else%}
<button type="submit" id="status" class="button" value="False"><span>follow</span></button>
{%endif%}jQuery:
<script>
$(document).ready(function(){
$(".button").click(function() {
var status = $("#status").val();
var course = $("#course").html()
//SECTION THAT I'M HAVING TROUBLE WITH
if ($(".button span").html() == "followed") {
$(".button").mouseover(function() {
$(".button span").html() = "unfollow";
}
}
$.ajax({
type: "POST",
url: "/follow",
data: 'status=' + status + "&course=" + course,
success: function() {
$(".button span").html($(".button span").html() == 'followed' ? 'follow' : 'followed');
$(".button").val($(".button").val() == 'True' ? 'False' : 'True');
}
});
return false;
});
});
</script>当我运行这个时,我得到
405 Method Not Allowed
The method POST is not allowed for this resource. 但是当我删除鼠标控制代码时,它就能工作了。鼠标控制代码有什么问题?
编辑:更新后的jQuery,除了onclick代码外,现在可以使用mouseout:
if ($(".button span").html() == "followed") {
$(".button").mouseover(function() {
$(".button span").html("unfollow");
});
$(".button").mouseout(function() {
$(".button span").html("followed");
});
}编辑:实际上,使用上面的代码,如果按钮被“跟随”,我点击它,按钮文本仍然是“跟随”。我认为这是因为mouseout函数正在覆盖ajax成功函数,该函数将其更改为“跟随”。单击按钮后,如何覆盖“跟随”鼠标输出函数?
发布于 2014-01-11 02:28:14
试一试,你错过了");“在你的鼠标通电话,这是硼化之后的一切.
//SECTION THAT I'M HAVING TROUBLE WITH
if ($(".button span").html() == "followed") {
$(".button").mouseover(function() {
// Pass the new string into .html()
$(".button span").html("unfollow");
});
}发布于 2014-01-11 02:31:38
$(document).ready(function(){
$('#status').hover(function() {
$(this).find('span').text('unfollow');
}, function() {
$(this).find('span').text('followed');
});
});发布于 2014-01-11 02:37:53
您应该尝试将JavaScript的使用限制在必要时。你可以通过普通的CSS实现同样的目标。下面是一个例子:
.button:hover .initialText, .hoverText {
display: none;
}
.button:hover .hoverText {
display: block;
}以及HTML:
<div class="button">
<span class="initialText">Followed</span>
<span class="hoverText">Unfollow</span>
</div>https://stackoverflow.com/questions/21057825
复制相似问题