在这种情况下,获取带有两个参数的函数的参数值的正确语法是什么。
起初,我的 removeRow(id) 函数只需要一个参数来进行处理。
html代码:
"<a href=javascript:removeRow("+sport.id+"); class='btn btn-xs btn-warning'>remove</a>"
js代码: //删除行
function removeRow(sportId) {
if ( 'undefined' != typeof sportId) {
console.log(sportId);
} else alert('Unknown id.');
}
现在,我希望这个函数有两个参数(语法???)
html代码:
"<a href=javascript:removeRow("+sport.id+","+ event.id+"); class='btn btn-xs btn-warning'>remove</a>"
js代码:
// Remove row
function removeRow(sportId,eventId) {
if ( 'undefined' != typeof sportId) {
console.log(sportId+ " " + eventId);
} else alert('Unknown id.');
}
您需要引用 ID 并像这样转义引号:
"<a href='javascript:removeRow(\""+sport.id+"\",\""+event.id+"\")' class='btn btn-xs btn-warning'>remove</a>"
但我强烈建议您不要使用 javascript href 并且使用数据属性代替
'<a href="#" onclick="return removeRow(this)" data-sportid="'+sport.id+'" data-eventid="'+event.id+'" class="btn btn-xs btn-warning">remove</a>'
并使用
function removeRow(link) {
var sportId = link.getAttribute("data-sportid"),
eventId = link.getAttribute("data-eventid");
if ( 'undefined' != typeof sportId) {
console.log(sportId+ " " + eventId);
}
else alert('Unknown id.');
return false; // cancel the link
}
不显眼地做同样的事情
window.onload=function() {
var sportLinks = document.querySelectoraAll(".sport");
for (var i=0;i<sportLinks.length;i++) {
sportLinks[i].onclick=function removeRow(e) {
e.preventdefault(); // cancel link event
var sportId = link.getAttribute("data-sportid"),
eventId = link.getAttribute("data-eventid");
if ( 'undefined' != typeof sportId) {
console.log(sportId+ " " + eventId);
}
else alert('Unknown id.');
}
}
}
添加一个类:
'<a href="#" data-sportid="'+sport.id+'" data-eventid="'+event.id+'" class="sport btn btn-xs btn-warning">remove</a>'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句