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

时间:2014-09-22 15:56:39

标签: javascript jquery html parent children

我有一个动态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()进行了检查,但我大部分时间都未定义,并且没有获得标题。

如何实现这一目标?

JSFIDDLEhttp://jsfiddle.net/hiteshbhilai2010/ywhbd9ut/14/

2 个答案:

答案 0 :(得分:1)

你想要使用:

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

alert(illFileTitle);

答案 1 :(得分:1)

见这个,

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