简体   繁体   中英

JavaScript create clickable href

I have a simple stupid question. I want to add a URL which is clickable in JavaScript to my HTML

var a = document.createElement('a');
a.setAttribute('href', 'http://example.at');
$("#upcomingEvents").append('Please check our website. ' + a);

The URL appears, but it is not clickable, how can I change that?

Thanks!

Try it like this:

$("#upcomingEvents").append('Please check our website. ');
$("#upcomingEvents").append(a);

The + operator causes the DOMNode to be cast to a string, you don't want that.

You have to put some text inside the link so there is something to click:

a.innerText='click me!';

And then you can't concatenate a string to a DOM element.

$("#upcomingEvents").append('Please check our website.');
$("#upcomingEvents").append(a);

Demo

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