创建href以调用函数;javascript";
我正在尝试使用javascript创建“a href”标记,但我不想将其用作web链接,而是想使用它来执行某些步骤!那我该怎么办创建href以调用函数;javascript";,javascript,Javascript,我正在尝试使用javascript创建“a href”标记,但我不想将其用作web链接,而是想使用它来执行某些步骤!那我该怎么办 var a = document.createElement('a'); a.attributes('href', 'function () { nn = 0 }' );// ? a.textContent = " " + pn.length + " "; document.getElementById("pagi
var a = document.createElement('a');
a.attributes('href', 'function () { nn = 0 }' );// ?
a.textContent = " " + pn.length + " ";
document.getElementById("pagingN").appendChild(a);
num[i] = num[i] + n;
在这里使用属性没有多大好处。只需将函数分配给
onclick
属性即可
var a = document.createElement('a');
a.onclick = function() { nn = 0; };
a.href = "#";
a.textContent = " " + pn.length + " ";
document.getElementById("pagingN").appendChild(a);
num[i] = num[i] + n;
请尝试:
a、 属性('href','javascript:function(){nn=0}');/
var a=document.createElement('a');
a、 setAttribute('href','javascript:hello()');/?
a、 textContent=“锚定”;
document.getElementById(“容器”).appendChild(a);
函数hello(){
警报(“你好世界”);
}
只需在href值中添加“javascript:”前缀,就可以通过执行类似的操作禁用锚定标记的默认行为-
a.onclick=function(e){
e.preventDefault();
}
注意:您可以使用一种非常方便的方法来添加事件-addListener,但为了简单起见,我还是使用onclick
a.onclick=function(e){
e.preventDefault();
}