我有一个函数,如果页面上的多个项目中的任何一个被单击,它就会运行。这个函数起作用了,我只想知道有没有更好的方法来使用||或类似的东西来列出它。
这就是我到目前为止所知道的:
Init: function () {
$('#indian-marker a').click(Operations.ShowIndia);
$('#map-2 a').click(Operations.ShowIndia);
$('#details-marker a').click(Operations.ShowIndia);
},
ShowIndia: function () {
$('#map').css('background-image', 'url("../img/map-background-india.jpg")');
$('#map-return').show();
$('#india-box-link').toggle();
$('#global-box-link').toggle();
return false;
}发布于 2011-12-23 18:55:24
,应该可以工作
Init: function () {
$('#indian-marker a, #map-2 a, #details-marker a').click(Operations.ShowIndia);
},
ShowIndia: function () {
$('#map').css('background-image', 'url("../img/map-background-india.jpg")');
$('#map-return').show();
$('#india-box-link').toggle();
$('#global-box-link').toggle();
return false;
}您可以从documentation查看此示例
发布于 2011-12-23 18:53:43
您可以组合选择器:
$('#indian-marker, #map2, #details-marker').find('a').click(Operations.ShowIndia);我已经将a部分从选择器移出到它自己的调用中,以避免它被重复。
另外,一定要小心那个回调函数。仅仅因为您将其称为Operations.ShowIndia并不意味着当它被实际调用时就是this === Operations。这样写会更安全:
... .click(function() {
this.ShowIndia();
});在这个特定的函数中这并不重要,但是如果您最终使用了其他依赖于this的函数,它将避免以后的混乱。
https://stackoverflow.com/questions/8615141
复制相似问题