简体   繁体   English

为什么onclick()在button标记内不起作用?

[英]Why onclick() didn't work inside the button tag?

I put the onclick() inside the button tag but it's give me an SyntaxError . 我将onclick()放在button标记内,但这给了我SyntaxError

I want to delete every rows if I click the button in the right corner of every rows. 如果我单击每行右上角的button ,我想删除每行。

var firstName = document.getElementById("txtFName");
var lastName = document.getElementById("txtLName");
var sex = document.getElementById("selSex");
var age = document.getElementById("txtAge");
var indice = [];
var id = 1;
var remove = "Remove";


var tblPerson = document.getElementById("tblPerson");

function remove(indice) { //remove row one by one
  for(var i = 0; i < indice.length; i++)
      myArray.splice(indice, 1);

}

function print() {
  var objPerson = new Person(id, firstName.value, lastName.value, selSex.value, age.value, remove); //instantiation 
  var display = "";


  display += "<tr>";

    display += "<td>" + objPerson._id + "</td>";
    display += "<td>" + objPerson._firstName + "</td>";
    display += "<td>" + objPerson._lastName + "</td>";
    display += "<td>" + objPerson._sex + "</td>";
    display += "<td>" + objPerson._age + "</td>";
    display += "<td>" + "<button onclick = "remove()">" + objPerson._remove + "</button>" + 
    "</td>"; //onclick gives me a SyntaxError


  display += "</tr>";
  tblPerson.innerHTML += display; //display the output many times
  id++; //increment id
}

Because you have a concatenation error in this line 因为这行有串联错误

display += "<td>" + "<button onclick = "remove()">" + objPerson._remove + "</button>"

which should be 应该是

display += "<td>" + "<button onclick = 'remove()'>" + objPerson._remove + "</button>"

or 要么

display += "<td>" + "<button onclick = \"remove()\">" + objPerson._remove + "</button>"

Except I seriously advise against concatenating html strings because it's not elegant and flexible at all. 除非我认真建议不要串联html字符串,因为它根本不优雅且不灵活。 Use document.createElement() instead. 请改用document.createElement()。

为什么onclick function在里面不起作用<div>苗条的标签?</div><div id="text_translate"><p> 我正在开发 VS 代码扩展。 为此,我使用 WebView 来显示用户界面并在其中执行一些任务。 所以我使用 svelte 来自定义我的 WebView 面板。</p><p> 我的问题是,我创建了包含 Styles、JS、HTML 的 svelte 文件。 因此,当我在标签内调用 onclick 方法时,它不起作用,但当它被放置在标签之外时,它就起作用了。 为什么会这样? 我该如何解决这个问题?</p><pre> <h1 style="text-align:center" on:click={microphoneOnAndOff}>Click Me</h1> **--// This work** <div class="box"> <div class="object"> <div class="outline" style="display: none;"></div> <div class="outline" id="delayed" style="display: none;"></div> <div class="buttonStyle"></div> <div class="buttonStyle" id="circlein"> <svg on:click={microphoneOnAndOff} class=""></svg> **--// This Not work** </div> </div> </div></pre><p> 谁能帮我解决这个问题?</p></div> - Why onclick function doesnt work inside <div> tag in svelte?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 onClick在表单内部不起作用? - onClick didn't work inside form? onclick 标签按钮在角度不起作用 - onclick tag button doesn't work in angular 为什么我的onclick功能没有起作用 - why my onclick function didn't work in react 为什么 styled-jsx 标签中的变量不起作用? - Why the variable in styled-jsx tag didn't work? 为什么onclick function在里面不起作用<div>苗条的标签?</div><div id="text_translate"><p> 我正在开发 VS 代码扩展。 为此,我使用 WebView 来显示用户界面并在其中执行一些任务。 所以我使用 svelte 来自定义我的 WebView 面板。</p><p> 我的问题是,我创建了包含 Styles、JS、HTML 的 svelte 文件。 因此,当我在标签内调用 onclick 方法时,它不起作用,但当它被放置在标签之外时,它就起作用了。 为什么会这样? 我该如何解决这个问题?</p><pre> <h1 style="text-align:center" on:click={microphoneOnAndOff}>Click Me</h1> **--// This work** <div class="box"> <div class="object"> <div class="outline" style="display: none;"></div> <div class="outline" id="delayed" style="display: none;"></div> <div class="buttonStyle"></div> <div class="buttonStyle" id="circlein"> <svg on:click={microphoneOnAndOff} class=""></svg> **--// This Not work** </div> </div> </div></pre><p> 谁能帮我解决这个问题?</p></div> - Why onclick function doesnt work inside <div> tag in svelte? 删除按钮不起作用 - Delete Button didn't work onClick事件在表单标记内不起作用 - onClick event doesn't work inside form tag 按钮内的 onChange 和 onClick 在 IE 上不起作用 - onChange and onClick inside a button doesn't work on I.E 窗体内具有onclick事件的普通Button不起作用 - normal Button with onclick event inside a form doesn't work 将按钮<a onclick>发送到另一个页面时</a> ,只有按钮onclick起作用, <a onclick>标记在javascript中不起作用</a> - Only button onclick works, <a onclick> tag doesn't work in javascript when sending parameter to another page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM