javascript - jQuery 根据其中的点击元素获取 <li> 的 ID

标签 javascript php jquery html css

任何人都知道如何使用 jQuery 根据其中单击的元素获取

  • 的 ID?
    这是我的 html 标签:

    <div id="grade_contamina">
        <ul id="ul_element">
            <!---My first li--->
            <li id="first_id">
                <p>
                    Apple
                </p>
                <!---Button for apple---->
                <span class="fruits" id="btn_apple">
                    <a class="btn_send_once">Send to Students</a>
                    <a class="btn_delete_once" >Delete</a>
                    <a class="btn_download_once">Download</a>
                </span>
            </li>
            <!---My second li--->
            <li id="second_id">
                <p>
                    Grapes
                </p>
                <!---Button for grapes---->
                <span class="fruits" id="btn_grapes">
                    <a class="btn_send_once">Send to Students</a>
                    <a class="btn_delete_once" >Delete</a>
                    <a class="btn_download_once">Download</a>
                </span>
            </li>
        </ul>
    </div>
    

    每个 LI 元素有 3 个按钮,删除、发送和下载。 比方说从“apple LI 元素”,我点击了删除。我需要根据其 id 删除此 LI 元素,或者我想根据其 id 发送此 base 。现在如何获取 LI 的 ID 或者获取 LI 元素 ID 的最佳方法是什么?

    除了获取 id 之外,我还想在单击删除按钮时触发 JQUERY 效果。

    I want this happen if the button delete is clicked.
    **Remove the LI element which is  clicked
    **Apped a new LI element at the last LI element of ul using "FadeIn Effect".
    

    这是我的伪代码

    if button is clicked{
       var the_id = get the id of LI where the button is clicked
       now which one of button is clicked?
       if button send is  clicked{
         some code here, i will need here the id to send...
       }else if button download is clicked{
         some code here, i will need here the id too..
       }else if button delete is clicked{
         remove the LI element (With fadeOut effect if possible)
         append a new LI element at the end of LI whith fadeIn effect
       }
       //End of Statement
    }
    

    谁能帮帮我?我对 jQuery 知之甚少,因此将不胜感激。谢谢你!!!

  • 最佳答案

    要在a 点击时获取liid,您需要使用closest。 这将获得被点击元素最近的 li 祖先。

    $('#grade_contamina').on('click', 'a', function() {
        alert($(this).closest('li').attr('id'));
        return false;
    });
    

    关于javascript - jQuery 根据其中的点击元素获取 <li> 的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30341198/

    相关文章:

    c# - 需要 javascript 验证下拉列表

    javascript - Live Update Callback -> afterTitle with Array via JSON file

    javascript - 使用javascript重命名数组中的对象键

    Javascript Date.Parse 在俄罗斯日期上崩溃

    javascript - 从 SQL 行访问键

    php - Yii2:减少 vServer 上的 inode 使用

    php - Silverstripe:清理 URL

    javascript - 用户打印时使用 jQuery 复制 HTML

    jquery - HTML5 将多个图像从一个 Canvas 拖放到另一个 Canvas

    php - SQL - 如何使用联接在同一个表(使用子查询)上执行交集?