如果按钮文本被"跟随"并且用户将鼠标悬停在它上面,我希望文本更改为"取消关注",并在用户停止悬停后更改回"跟随".
按钮:
{%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%}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
当我跑这个我得到
405 Method Not Allowed
The method POST is not allowed for this resource.
Run Code Online (Sandbox Code Playgroud)
但当我删除鼠标悬停代码时,它的工作原理.鼠标悬停代码有什么问题?
编辑:使用mouseout更新现在可以在onclick代码之外工作的jQuery:
if ($(".button span").html() == "followed") {
$(".button").mouseover(function() {
$(".button span").html("unfollow");
});
$(".button").mouseout(function() {
$(".button span").html("followed");
});
}
Run Code Online (Sandbox Code Playgroud)
编辑:实际上,使用上面的代码,如果按钮被"跟随"并且我点击它,按钮文本仍然是"跟随".我认为这是因为mouseout函数覆盖了ajax成功函数,将其更改为"follow".单击按钮后,如何覆盖"跟随"鼠标输出功能?
试试以下,你错过了");" 在你的鼠标悬停电话上,之后就是把所有事情都搞砸了......
//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");
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5960 次 |
| 最近记录: |