简体   繁体   中英

Onclick not working with button created in JavaScript

I made a function that puts a button in a leaflet popup like

function popUp(feature, json){
    myfunc("Cat").outerHTML
};

Here's the function that creates the button

function myfunc(String) {
button = document.createElement('button');
button.textContent = String
button.onclick = function dog() {
    alert("dog");
    return false;
};
document.body.appendChild(button);
  return button
};

The button appears and is clickable in the popup but it doesn't do anything when clicked. I allowed pop-ups in Chrome and even tried it in Firefox but it still won't work.

Also, these two functions are in a functions.js file separate from my index.html file.

Try removing return false from button function

button.onclick = function dog() {
    alert("dog");
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM