单击一次后禁用href并同时添加新元素?

时间:2016-06-19 01:30:52

标签: javascript jquery html


是否可以通过使用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()事件处理程序来解决问题。 感谢您的反馈人员

4 个答案:

答案 0 :(得分:1)

您可以使用.one();请注意问题js)

处缺少右括号click()
 $(document).ready(function() {
   $("#add_app").one("click", function() {
     $("#box").append("<p>jQuery is Amazing...</p>"); 
     this.removeAttribute('href');this.className='disabled';
   })
});

答案 1 :(得分:1)

使用这个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/

答案 2 :(得分:1)

你可以试试这个

<强> HTML

<a style="cursor: pointer;"  id="add_app" ><strong>Summon new element and disable me</strong></a>
<div id="box"></div>

在你的JS代码中你可以写这个我测试过它并且工作正常

$(function(){
  $("#add_app").one("click",function(){
    $("#box").append("<p>jQuery is Amazing...</p>");
  });
 });

答案 3 :(得分:0)

你需要返回单击链接的假whwn。 试试这个JQuery代码..

$("#add_app").click(function() {
   $("#box").append("<p>jQuery is Amazing...</p>");
   return false;
});