目前,我正在使用jQuery执行一个click
函数,检查单击的元素是否有一个属性等于三个不同的值。
有没有一种更整洁的书写方式?
$('body').on('click','svg a', function(e){
if ($(this).attr('xlink:href') =='#congo' || $(this).attr('xlink:href') =='#onshore' || $(this).attr('xlink:href') =='#sea'){
e.preventDefault();
}
});
发布于 2014-09-24 14:35:03
也许是$.inArray解决方案?
$.inArray($(this).attr('xlink:href'), ['#congo', '#onshore', '#sea']) != -1
发布于 2014-09-24 14:37:46
['#congo','#onshore','#sea'].indexOf($(this).attr('xlink:href')) > -1
发布于 2014-09-24 14:44:20
与regex (纯JS)
$(document.body).on('click','svg a', function(e){
var a = $(this).attr('xlink:href'),
match = a.match(/#congo|#onshore|#sea/);
if( match && match[0] == a )
e.preventDefault();
});
https://stackoverflow.com/questions/26019583
复制相似问题