编辑:“更多”链接是从这里构建的:
document.getElementById("elenco_fiveboard").insertAdjacentHTML('beforeend',
"<li>" +
"Titolo: " + valore_comando + " " +
"(<a href='javascript:getInfo("+ valore_comando +");'>" +
"more" + "</a>)" +
"</li>");
所以我必须用连字符包裹 valore_comando 但尝试写入时出错
"(<a href='javascript:getInfo(""+ valore_comando +"");'>" +
或
"(<a href='javascript:getInfo('"+ valore_comando +"');'>" +
抱歉,我对 JS 语法不太熟悉,而且我从一些不属于我自己的代码开始。
我有这个简单的 JS 函数:
getInfo = function(title){
worker.port.postMessage("maggiori_informazioni:" + title);
}
我运行它,将一个值传递给变量标题,但总是收到错误。如果标题是“example”,则 JS 尝试运行
getInfo = function(example)
我收到错误:
Reference Error: example is not defined. If title has more than one word: 'first example' then JS will try to run
getInfo = function(first example)
我收到错误:
Syntax error: missing ) after argument list (looking for parentesys after 'first').
我在这里缺少什么? 这段代码是我第一次使用 webSockets 进行测试的一部分。如果你想查看完整的代码可以打开这个index page并为您看到的第一个警报赋值。该值是您将在 dashboard page 上看到的标题。 。 尝试在仪表板中每个定义的标题后点击“更多”,可以重现该错误。 抱歉网站中显示意大利语,但这是用户要求。
最佳答案
getInfo
引用您的函数。所以你可以通过以下方式调用它:
getInfo(value);
Use quotes if you are passing string value and if you are passing integer value you can simply pass.
例如:
getInfo("Message Title"); //string
getInfo(3); // Integer
<小时/>
您也可以这样调用:
getInfo = function(title){
alert("maggiori_informazioni:" + title);
}("String");
更新:
你可以使用这个:
"(<a href='"+getInfo(valore_comando)+";'>" +
关于javascript - 将值传递给函数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31947744/