gpt4 book ai didi

javascript - 单击更改字形图标

转载 作者:行者123 更新时间:2023-11-30 20:52:54 25 4
gpt4 key购买 nike

我试图在单击时更改此字形图标,但当 href = "#"时它工作正常,但最初我的 href 包含一个 url,它通过 View 访问 django 中的数据库。

HTML:

<div class='hidden-lg'>

<div class="content-header">
<h1>
<a id = "star" href="#" data-toggle="tooltip" title = "Add to your list" data-placement="top">
<span class="glyphicon glyphicon-star-empty"></span>
</a>
</h1>

</div>
</div>

JS:

$('#star').click( function(){

$(this).find('span').toggleClass('glyphicon glyphicon-star-empty').toggleClass('glyphicon glyphicon-star');

});

这很好用。 http://jsfiddle.net/asees/5XqyW/564/

但是当我将 href 更改为以下代码时,glyphicon 没有变化。

<a id = "star" href="{% url 'add' %}" data-toggle="tooltip" title = "Add to your list" data-placement="top">
<span class="glyphicon glyphicon-star-empty"></span>
</a>

最佳答案

Working fiddle .

由于您点击的是 anchor #star,因此您需要阻止默认事件 (重定向到 href)使用 e.preventDefault() 像:

$('#star').click( function(e){
e.preventDefault();

$(this).find('span').toggleClass('glyphicon glyphicon-star-empty').toggleClass('glyphicon glyphicon-star');
});

关于javascript - 单击更改字形图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47980240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com