开发者

Jquery - how to use $()

How do I convert the following javascript to using JQuery?

document.getElementById("asc").removeAttribute("href");
document.getE开发者_高级运维lementById("asc").onclick = "";
document.getElementById("asc").style.textDecoration = "underline"

I think I'm close using the below code but this doesn't quite work.

$('#asc').attr('href', '').click(function() {return false}).css('text-decoration', 'underline');


Why not just

$('#asc').replaceWith($('#asc').text())

which will replace the link with just ordinary text, and save you having to worry about all the aspects of a link.


The only differences I can see is that the href attribute isn't actually being removed. You're also creating an event handler when the first example doesn't have one.

This will remove the attribute instead:

$('#asc').removeAttr('href').css('text-decoration', 'underline');

If there's already an onclick handler on it, try this:

$('#asc').removeAttr('href').attr('onclick', '').css('text-decoration', 'underline');


If you can't decide between two of them you want the following...

$('#asc').removeAttr('href').click(function() {return false}).css({'text-decoration' : 'underline'});


why assigning a new event handler if you want to get the rid of it

$("#asc")
.attr("href", "")
.unbind("click")
.attr("style", "text-decoration:underline");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜