如何使用jQuery通过Id找到特定的父级,并通过某个类获取其特定子级的值

How to find a particular grand parent by Id and get value of its particular children by certain class using jQuery

本文关键字:获取 通过 jQuery 何使用 Id      更新时间:2023-09-26

我有一个动态div

<div class="illustartionWrap" id="illustartionWrapId">
     <div class="illusList illusListTop" id="illusList_heading">
            <span class="fileName">File Name</span>
            <span class="fileDesc">Description</span>
            <span class="fileSize">File Size</span>
            <span class="fileDownload">Download</span>
    </div>
    <div class="clear"></div>
    <div class="illusList">
            <span class="fileName">fileTitle</span>
            <span class="fileDesc">fileDesc</span>
            <span class="fileSize">1.18 MB</span>
            <span class="fileDownload">
                <a href="http://myfile/file.txt">
                    <img src="/sites/all/themes/petheme/images/myimage.png">
                </a>
                <a href="#">
                    <img id="illFile_6" class="illDeleteFile" src="/sites/all/themes/petheme/images/icons/deleteButton.png">//clicking it to delete the file from db 
                </a>
            </span>
        </div>
 </div>

如何获取此删除按钮的父级并找到filetitle类以获取文件的标题。

下面是我写的点击处理程序

/**delete function for my page**/
    jQuery(".illDeleteFile").on("click", function() {
        var illDeleteId = jQuery(this).attr("id").split("_");
        var illFileTitle = jQuery(this).parent("#illusList").children(".fileName").val();
        alert (illFileTitle);
});

我检查了jQuery Parent()、Parents()和children()以及find(),但我大部分时间都没有定义,也没有得到标题。

如何做到这一点?

JSFIDDLE:http://jsfiddle.net/hiteshbhilai2010/ywhbd9ut/14/

看看这个,

jQuery(".illDeleteFile").on("click", function() {
    var illFileTitle =jQuery(this).closest(".illusList").find(".fileName").html();
    alert (illFileTitle);
});

您需要使用:

var illFileTitle = jQuery(this).closest(".illusList").find(".fileName").text();
alert(illFileTitle);