[英]JQuery Toggle True or False on data-filter
我有一個圖像鏈接,我想用作切換按鈕。
<img id="myToggleButton" src="myToggle.jpg" />
And what I want it to toggle is this:
<ul id="listview" data-filter="true">
So, basically I need to change from data-filter="true" or data-filter="false" and so on.
如何使用JQuery使其正常工作?
$('#myToggleButton').click(function(){
var $listview = $('#listview');
$listview.attr('data-filter',
$listview.attr('data-filter')=='false' ? 'true' : 'false'
);
});
有什么類似的?
var $list = $('#listview');
$('#myToggleButton').click(function (event) {
if($list.data('filter') == true) {
$list.attr('data-filter', false);
}else {
$list.attr('data-filter', true);
}
});
這是一種使用data()
方法處理數據屬性的更簡潔現代的jQuery方法:
$('#myToggleButton').click(function (){
var listview = $('#listview');
listview.data('filter', !listview.data('filter'));
listview.listview('refresh');
});
這也是一個JSFiddle ,因此您可以在實際中看到它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.