简体   繁体   中英

changing anchor tag into button with javascript/jquery mobile?

I'm trying to change my anchor tag to a button and have it function properly but it seems when I change this:

var editButton = document.createElement('a');


        editButton.href = "#additem";

this:

var editButton = document.createElement('button');


        editButton.href = "#additem";

The button when clicked, doesn't do anything. If I switch it back however, the link works fine... my goal is essentially to change the anchor text into an actual jquery button.

I've also tried setting the attribute class to ui button but that did nothing as well. Any help would be greatly appreciated.

The button does not have a property of href, what you can do is lsten to the onclick event of the button like this:

var editButton = document.createElement('button'); 
editButton.onclick = function(e){ location.href = 'http://' + window.location.hostname + window.location.pathname +'#additem'}

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