简体   繁体   English

使用jQuery在链接周围添加div

[英]adding div around link using jquery

I am adding a div around a link on click of a button. 单击按钮后,我在链接周围添加了一个div。 but when i click button multiple times, it adds multiple divs. 但是当我多次单击按钮时,它会添加多个div。

    <li>
    <label> </label>
    <div class="deletebutton">
        <label> </label>
        <div class="deletebutton">
            <label> </label>
            <div class="deletebutton">
<input type="button" id="ctl00_ContentPlaceHolder1_ctrlAddPhotos_RadUpload1remove1" value="Remove" class="ruButton ruRemove" name="RemoveRow">
            </div>
        </div>
    </li>

How can i make sure that it first checks if there is a div around link and then adds. 我如何确保它首先检查链接周围是否有div,然后添加。

I am using following code: 我正在使用以下代码:

var parentTag = $(".ruRemove").parent().get(0).tagName;

if (parentTag == 'LI') {
    $(".ruRemove").wrap("<div class='data deletebutton'></div>");
    $(".deletebutton").before("<label></label>");
} else {

    var par = $('.deletebutton').parent();
    if (par.is('div')) par.remove();
    $(".ruRemove").wrap("<div class='data deletebutton'></div>");

    var prev = $('.deletebutton').prev();
    if (prev.is('label')) prev.remove();
    $('.deletebutton').before("<label></label>");
}

it should become this: 它应该变成这样:

  <li>
                <label> </label>
                <div class="deletebutton">
    <input type="button" id="ctl00_ContentPlaceHolder1_ctrlAddPhotos_RadUpload1remove1" value="Remove" class="ruButton ruRemove" name="RemoveRow">
                </div>
        </li>

when i click button. 当我单击按钮。 before clicking html is: 在单击html之前是:

  <li>             
    <input type="button" id="ctl00_ContentPlaceHolder1_ctrlAddPhotos_RadUpload1remove1" value="Remove" class="ruButton ruRemove" name="RemoveRow">  
        </li>

Here is a solution shown in a jsFiddle . 这是jsFiddle中显示的解决方案。

The code story is 代码故事是

HTML 的HTML

<button id="myButton">My Button</button

JavaScript 的JavaScript

$(function() {
    $("#myButton").click(function() {
        if ($(this).parent().get(0).tagName !== "DIV") {
            $(this).wrap("<div class='myDiv'>");
        }
    });
});

What the code does is register a callback for a button click. 该代码的作用是为按钮单击注册回调。 When clicked, we ask for the parent of the button that was clicked and ask if the parent node has a tag name of "DIV" meaning it is a <div> . 单击时,我们询问被单击按钮的父节点,并询问父节点的标签名称是否为“ DIV”,这意味着它是<div> If it is not a div, then we wrap the button in a div and end. 如果不是 div,则将按钮包装在div中并结束。 On the next call, the detection of the parent being a div will be true and no new div will be added. 在下一次调用时,将检测到父级为div,并且不会添加新的div。

Why don't you just use for example a function that does what you want only on the first click? 例如,为什么不只使用仅在第一次单击时就能完成所需功能的功能?

So only on the first click of that button adds the div, if you click other times the button, it wont do anything. 因此,仅在该按钮的第一次单击上添加了div,如果您单击其他次按钮,它将不会执行任何操作。 This way you wont add multiple divs. 这样,您就不会添加多个div。

To do that you could use for example jQuery: 为此,您可以使用例如jQuery:

<script type="text/javascript">

    $("#firstclick").one("click",function() {
        alert("This will be displayed only once.");
    });

</script>

You can check even the jQuery API Documentation regarding one: 您甚至可以查看有关以下内容的jQuery API文档:

http://api.jquery.com/one/ http://api.jquery.com/one/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM