单击一次后禁用href并同时添加新元素?(Disable a href after click once and add new element at the same time?)

编程入门 行业动态 更新时间:2024-10-25 16:20:43
单击一次后禁用href并同时添加新元素?(Disable a href after click once and add new element at the same time?)

是否可以通过使用href添加新元素并同时禁用href? 所以href只点击一次,之后会出现新元素。 我写了这段代码,但仍然没有运气

JS

$(document).ready(function() { $("#add_app").click(function() { $("#box").append("<p>jQuery is Amazing...</p>"); this.removeAttribute('href');this.className='disabled'; }});

HTML

<a href="#" id="add_app" ><strong>Summon new element and disable me</strong></a>

唯一的工作是JS系列

$("#box").append("<p>jQuery is Amazing...</p>");

需要帮助..谢谢

-------------------------------------- UPDATE ----------- ---------------------- - 使用PacMan,Linus Aronsson和guest271314改进的代码,它就像一个魅力。 所以基本上我们使用.one()事件处理程序来解决问题。 谢谢你们的反馈

Is it posible to add new element with javascript by using href and disable href at the same time? So href only clicked once, and after that the new element will appear. I wrote this code, but still no luck

JS

$(document).ready(function() { $("#add_app").click(function() { $("#box").append("<p>jQuery is Amazing...</p>"); this.removeAttribute('href');this.className='disabled'; }});

HTML

<a href="#" id="add_app" ><strong>Summon new element and disable me</strong></a>

The only does the job is in JS line

$("#box").append("<p>jQuery is Amazing...</p>");

Need help.. Thanks

--------------------------------------UPDATE--------------------------------- -Using improved code by PacMan, Linus Aronsson, and guest271314 and it works like a charm. So basically we use .one() event handler to solve the problem. Thanks for the feedback guys

最满意答案

使用这个jQuery:

$(document).ready(function() { $("#add_app").one("click", function() { $("#box").append("<p>jQuery is Amazing...</p>"); this.removeAttribute('href'); }); });

我正在使用.one()事件处理程序来指示您只希望click事件发生一次。 其余的代码或多或少是正确的,只有一些小的改动。

这是一个小提琴: https : //jsfiddle.net/0mobshpr/1/

Use this jQuery:

$(document).ready(function() { $("#add_app").one("click", function() { $("#box").append("<p>jQuery is Amazing...</p>"); this.removeAttribute('href'); }); });

I'm using the .one() event handler to indicate that you only want the click event to take place once. The rest of the code was more or less correct with a few small changes.

Here's a fiddle: https://jsfiddle.net/0mobshpr/1/

更多推荐

本文发布于:2023-08-05 20:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1438370.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   新元素   href   Disable   time

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!