英文:
JavaScript onClick handler throwing error
问题
下面是翻译好的部分:
我有以下正常工作的锚点标签。
以下是函数调用。
我想将此函数更改为接受两个参数。
我收到以下错误。Id 是整数,type 是字符串。不确定是否重要。我从两者中都移除了 ' ',但仍然收到相同的错误。
如果您需要更多的帮助,请随时告诉我。
英文:
I have the below anchor tag which is working fine.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
"<a href='#' title='Move' onclick='change_type(" + id +");return false;'><img height='20' width='28' src='" + images + "/move.png' alt='Move' /></a>"
<!-- end snippet -->
Here is the function call
function change_type(id) {
alert(id);
}
I want to change this function to accept two parameters.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
"<a href='#' title='Move' onclick='change_type(" + id + ", '" + loan_type +"');return false;'><img height='20' width='28' src='" + images + "/move.png' alt='Move' /></a>"
<!-- end snippet -->
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
function change_type(id, type) {
alert(id + type);
}
<!-- end snippet -->
I am getting below error. Id is int, type is string. Not sure if it matters. I removed and '' to both of them and still getting the same error
答案1
得分: 1
由于您在onclick
属性内部使用单引号来表示字符串,您应该在onclick
本身的内容中使用双引号来括起来。
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let id = 123, loan_type = "loan_type", images="https://via.placeholder.com/50x50";
document.write("<a href='#' title='Move' onclick=\"change_type(" + id + ", '" + loan_type +"');return false;\"><img height='20' width='28' src='" + images + "/move.png' alt='Move' /></a>");
function change_type(id, type) {
alert(id + type);
}
<!-- end snippet -->
或者,您可以使用HTML实体&#39;
或&apos;
来表示loan_type
周围的单引号,以免它被解释为结束onclick
属性。
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let id = 123, loan_type = "loan_type", images="https://via.placeholder.com/50x50";
document.write("<a href='#' title='Move' onclick='change_type(" + id + ", &#39;" + loan_type +"&#39;);return false;'><img height='20' width='28' src='" + images + "/move.png' alt='Move' /></a>");
function change_type(id, type) {
alert(id + type);
}
<!-- end snippet -->
英文:
Since you're using single quotes for a string inside the onclick
attribute, use double quotes for enclosing the content of onclick
itself.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let id = 123, loan_type = "loan_type", images="https://via.placeholder.com/50x50";
document.write("<a href='#' title='Move' onclick=\"change_type(" + id + ", '" + loan_type +"');return false;\"><img height='20' width='28' src='" + images + "/move.png' alt='Move' /></a>");
function change_type(id, type) {
alert(id + type);
}
<!-- end snippet -->
Alternatively, use the HTML entity &#39;
or &apos;
for the single quotes around loan_type
so that it is not interpreted as ending the onclick
attribute.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let id = 123, loan_type = "loan_type", images="https://via.placeholder.com/50x50";
document.write("<a href='#' title='Move' onclick='change_type(" + id + ", &#39;" + loan_type +"&#39;);return false;'><img height='20' width='28' src='" + images + "/move.png' alt='Move' /></a>");
function change_type(id, type) {
alert(id + type);
}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论