JavaScript onClick handler throwing error

huangapple go评论63阅读模式
英文:

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 -->

&quot;&lt;a href=&#39;#&#39; title=&#39;Move&#39; onclick=&#39;change_type(&quot; + id +&quot;);return false;&#39;&gt;&lt;img height=&#39;20&#39; width=&#39;28&#39; src=&#39;&quot; + images + &quot;/move.png&#39; alt=&#39;Move&#39; /&gt;&lt;/a&gt;&quot;

<!-- 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 -->

&quot;&lt;a href=&#39;#&#39; title=&#39;Move&#39; onclick=&#39;change_type(&quot; + id + &quot;, &#39;&quot; + loan_type +&quot;&#39;);return false;&#39;&gt;&lt;img height=&#39;20&#39; width=&#39;28&#39; src=&#39;&quot; + images + &quot;/move.png&#39; alt=&#39;Move&#39; /&gt;&lt;/a&gt;&quot;

<!-- 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

JavaScript onClick handler throwing error

JavaScript onClick handler throwing 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实体&amp;#39;&amp;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 + ", &amp;#39;" + loan_type +"&amp;#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 = &quot;loan_type&quot;, images=&quot;https://via.placeholder.com/50x50&quot;;
document.write(&quot;&lt;a href=&#39;#&#39; title=&#39;Move&#39; onclick=\&quot;change_type(&quot; + id + &quot;, &#39;&quot; + loan_type +&quot;&#39;);return false;\&quot;&gt;&lt;img height=&#39;20&#39; width=&#39;28&#39; src=&#39;&quot; + images + &quot;/move.png&#39; alt=&#39;Move&#39; /&gt;&lt;/a&gt;&quot;);
function change_type(id, type) {
    alert(id + type);
}

<!-- end snippet -->

Alternatively, use the HTML entity &amp;#39; or &amp;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 = &quot;loan_type&quot;, images=&quot;https://via.placeholder.com/50x50&quot;;
document.write(&quot;&lt;a href=&#39;#&#39; title=&#39;Move&#39; onclick=&#39;change_type(&quot; + id + &quot;, &amp;#39;&quot; + loan_type +&quot;&amp;#39;);return false;&#39;&gt;&lt;img height=&#39;20&#39; width=&#39;28&#39; src=&#39;&quot; + images + &quot;/move.png&#39; alt=&#39;Move&#39; /&gt;&lt;/a&gt;&quot;);
function change_type(id, type) {
    alert(id + type);
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月4日 05:59:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632226.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定