Ale*_* G. 11 tags jquery href jquery-animate
我有以下代码
<a href="" (set value 1)>Inside Link which sets a value</a>
<script>
$(a).click(function() {
i=value of a tag;
$('#square').animate({'left': i * 360});
});
</script>
Run Code Online (Sandbox Code Playgroud)
我想为锚标签添加一个值属性.怎么做?
And*_*man 27
如果要为值添加随机属性,可以使用数据属性:
<a href="#" data-value="1">Text</a>
<script type="text/javascript">
$("a").click(function(){
i=$(this).data("value");
$('#square').animate({'left': i * 360});
});
</script>
Run Code Online (Sandbox Code Playgroud)
Oyb*_*bek 10
如果您使用的是HTML5,则可以使用该data-技术.
<a id="target" href="http://foo.bar" data-custom-value="1">Text</a>
$("#target").click(function() {
var value = $(this).data("custom-value");
// do other stuff.
});
Run Code Online (Sandbox Code Playgroud)
编辑
使用.data而不是.attr更合适
| 归档时间: |
|
| 查看次数: |
40741 次 |
| 最近记录: |