与Js不太好,所以寻找一些现有代码的帮助。 我有以下锚点
<a href="#" class="addroom" onclick="return false;"><span>Add</span></a>
我收到关于'onclick'事件的警告,它告诉我我没有onclick的键盘等效处理程序=“返回false;我做了一些研究,我可以通过使用preventDefault来防止此警告。我把它放在页面的脚本标签中然后它的工作方式相同,我认为它将解决问题。
$("a.addrom").click(function(e) {
e.preventDefault();
});
但是,我更愿意将它添加到现有的js中,但我很难解决最新情况。我正在尝试将其添加到点击事件中。
setupRooms: function (settings) {
//hide all age fields
$(settings.agesSelector, settings.hotelSearchDiv).hide();
//hide all except first
$(settings.roomsSelector + ":not(:first)", settings.hotelSearchDiv).hide();
$('select', settings.hotelSearchDiv).prop('selectedIndex', 0); //set all to 0
$(settings.addRoomSelector, settings.hotelSearchDiv).on('click', function () {
methods.addRoom(settings);
});
$(settings.removeRoomSelector, settings.hotelSearchDiv).on('click', function () {
var id = $(this).data('id');
methods.removeLastRoom(settings, id);
});
$(settings.childrenNumberSelector, settings.hotelSearchDiv).on('change', function () {
methods.handleChildrenChange(settings, $(this));
});
},
编辑*这个代码对我有用,感谢@patrick&amp; @roberto
$(settings.addRoomSelector, settings.hotelSearchDiv).on('click', function (e) {
e.preventDefault();
methods.addRoom(settings);
});
答案 0 :(得分:2)
如果我理解正确,你想在你的点击处理程序上添加它:
$(settings.addRoomSelector, settings.hotelSearchDiv).on('click', function (e) {
e.preventDefault();
methods.addRoom(settings);
});
$(settings.removeRoomSelector, settings.hotelSearchDiv).on('click', function (e) {
e.preventDefault();
var id = $(this).data('id');
methods.removeLastRoom(settings, id);
});
足以在点击处理程序中设置防止默认值。
干杯