4

我一直很难找到正确的选择器,而且我刚刚通过 do 让它工作.parent().parent().siblings(),但我知道必须有更好的方法来获得它,而不需要太多链接。

div 是动态添加的,这是我发现选择它的唯一可靠方法:

var current = $(".filename:contains('" + file.name + "')").parent().parent().siblings();
current.find("input[name=title]").val(obj.file_name);

这是 jsFiddle 链接:http: //jsfiddle.net/Msnf9/8/

这是 HTML:

<div id="uploadifive-fileupload-queue" class="uploadifive-queue">
    <div class="uploadifive-queue-item" id="uploadifive-fileupload-file-0">
        <div class="span12 well">
            <div class="row-fluid">
                <div class="alert">
                    <div class="filename">file-name-1.jpg</div>
                    <div class="fileinfo"> - Completed</div>
                </div>
                <div class="progress">
                    <div class="bar"></div>
                </div>
            </div>

            <div class="row-fluid inputs">
                <div class="span3">
                <ul class="thumbnails">
                    <li class="span12">
                      <a href="#" class="thumbnail">
                        <img src="http://placehold.it/260x180" alt="">
                      </a>
                    </li>
                  </ul>
                </div>
                <div class="span9">
                    <form class="form-horizontal">
                        <fieldset>
                            <div class="control-group">
                                <label class="control-label" for="file-name">File Name</label>
                                <div class="controls">
                                    <span class="input-xlarge uneditable-input file-name" /></span>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="file-dimensions">File Dimensions</label>
                                <div class="controls">
                                    <span class="input-xlarge uneditable-input file-dimensions" /></span>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="file-url">File URL</label>
                                <div class="controls">
                                    <span class="input-xlarge uneditable-input file-url" /></span>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="alt-text">Alt Text</label>
                                <div class="controls">
                                    <input type="text" class="input-xlarge" placeholder="Alt text" name="alt-text" id="alt-text" />
                                </div>
                            </div>
                            <input type="hidden" name="image_id" />
                            <div class="form-actions">
                                <button id="save" type="button" class="btn btn-primary"><i class="icon-ok icon-white"></i> Save</button>
                                <button id="delete" type="button" class="btn btn-inverse"><i class="icon-trash icon-white"></i> Delete</button>
                         </fieldset>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
4

3 回答 3

6

您可以使用closest()方法:

获取与选择器匹配的第一个元素,从当前元素开始,向上遍历 DOM 树。

var current = $(".filename:contains('" + file.name + "')").closest('.row-fluid').siblings();
于 2012-07-12T18:02:30.413 回答
2

使用最接近()方法,它为您提供 DOM 中元素的最近祖先。当您row-fluid与所需的父母上课时,该课程用于到达所需的父母。

现场演示

var current = $(".filename:contains('file-name-1.jpg')").closest('.row-fluid').siblings();
于 2012-07-12T18:04:52.297 回答
0

我喜欢最亲近的,然后是孩子。

var current = $(".filename:contains('" + file.name + "')").closest('.row-fluid').siblings()
于 2012-07-12T18:01:18.130 回答