Javascript onclick参数

Javascript onclick参数,javascript,variables,Javascript,Variables,我对JavaScript中的“onclick”函数有一个问题。这里有一个div“信息栏” 两个用于循环 var src = new Array(); for(var i = 0; i < 2; i++){ src.push("el1","el2"); } for(var j = 0; j < 2; j++){ doesFileExist(src[j]); } var src=new Array(); 对于(变量i=0;i

我对JavaScript中的
“onclick”
函数有一个问题。这里有一个div
“信息栏”


两个用于循环

 var src = new Array();

 for(var i = 0; i < 2; i++){
    src.push("el1","el2");
 }

 for(var j = 0; j < 2; j++){
    doesFileExist(src[j]);
 }
var src=new Array();
对于(变量i=0;i<2;i++){
src.push(“el1”、“el2”);
}
对于(var j=0;j<2;j++){
doesFileExist(src[j]);
}
以及doesFileExist()和klick函数

function klick(el){
    alert(el)
}

function doesFileExist(urlToFile){
    document.getElementById('InfoBar').innerHTML += '<br>' + '<a id="css" onclick="klick(urlToFile)" href="#" title="'+urlToFile+'">' + "link1 : "  + urlToFile + '</a>';
}
函数klick(el){
警报(el)
}
函数doesFileExist(urlToFile){
document.getElementById('InfoBar').innerHTML++='
'+''; }
现在我在
“a href”
中添加了一个
“onclick”
函数

如果单击“link1:el1”,我希望显示为警报
“urlToFile”
字符串。 但我不工作

“a href”
title=“+urlToFile+”
中,它工作得很好,但在
中,“onclick”
不起作用

有人能帮我吗?
提前感谢。

以这种方式分配的事件句柄将不起作用。您必须使用JavaScript事件句柄。也就是说,必须创建一个新的“a”元素,然后将单击事件绑定到该元素,然后将其作为子元素附加到父节点。所有这些东西在网上都有很好的描述。

您正在生成一个属性。它被转换回函数,但作用域被破坏

  • 不要使用内部事件属性
  • 尽量减少使用globals
  • 避免通过将字符串混合在一起生成HTML(最好是很难阅读,最坏的情况是你会遇到这种问题)
使用标准DOM:

var container = document.getElementById('InfoBar');
container.innerHTML = ""; // Delete any existing content
container.appendChild(document.createElement('br'));
var anchor = document.createElement('a');
anchor.setAttribute('id', 'css'); // You are running this function is a loop and creating duplicate ids. Use a class instead.
anchor.addEventListener('click', function (event) {
    klick(urlToFile); // the local variable urlToFile is still in scope
});
anchor.setAttribute('href', '#'); // Why are you linking to the top of the page? Use a <button>
anchor.setAttribute('title', urlToFile);
anchor.appendChild(document.createTextNode("link1 : " + urToFile));
container.appendChild(anchor);
var container=document.getElementById('InfoBar');
container.innerHTML=“”;//删除任何现有内容
container.appendChild(document.createElement('br'));
var anchor=document.createElement('a');
anchor.setAttribute('id','css');//您正在以循环方式运行此函数并创建重复ID。改为使用类。
anchor.addEventListener('click',函数(事件){
klick(urlToFile);//局部变量urlToFile仍在作用域中
});
anchor.setAttribute('href','#');//为什么要链接到页面顶部?使用
setAttribute('title',urlToFile);
appendChild(document.createTextNode(“link1:+urToFile”);
容器.附件(锚);

klick fire中是否有警报?我怀疑您的klick函数期望通过eventPost调用时未定义的“el”klick(urlToFile)函数或创建id的代码必须是唯一的-
id=“css”
至少设置了两次
var container = document.getElementById('InfoBar');
container.innerHTML = ""; // Delete any existing content
container.appendChild(document.createElement('br'));
var anchor = document.createElement('a');
anchor.setAttribute('id', 'css'); // You are running this function is a loop and creating duplicate ids. Use a class instead.
anchor.addEventListener('click', function (event) {
    klick(urlToFile); // the local variable urlToFile is still in scope
});
anchor.setAttribute('href', '#'); // Why are you linking to the top of the page? Use a <button>
anchor.setAttribute('title', urlToFile);
anchor.appendChild(document.createTextNode("link1 : " + urToFile));
container.appendChild(anchor);