Roy*_*mir 4 javascript jquery jquery-plugins jquery-selectors
假设我有这种结构(没有类,没有Id): - 这是一个定位问题
d [0,1,2]是纯粹的divs.没有班级没有id.
div(d0)包含一个重复的结构,如:

让我们说我有$(this)第一个 d1
获取其他2有任何遍历功能,方法和解决方案d1吗?(或所有d1's.无关紧要..)
我会用语言来嘲笑它:
我
$(this)=d1.我有一个父(d2可以更多......这只是我的样本)有一个父(d3)因此,我有2个更像我的精确元素.
我怎么才能得到它们?JSBIN
也许我不清楚,但我不必知道结构.我只是有$(this)它应该找到其他双胞胎相对于d0(与其位置相同)
所以应该是这样的:
function getLikeMyself(objWhichIsThis , contextDivElement)
{
}
execute : getLikeMyself(theDivWhishIsThis, divElemtnWhichIs_d0)
Run Code Online (Sandbox Code Playgroud)
这是我的新尝试,这个.me()方法将返回当前构建的元素的选择器,直到body.
$.fn.me = function(){
return this.first().parentsUntil("body").andSelf().map(function(){
return this.tagName;
}).get().join(">");
};
Run Code Online (Sandbox Code Playgroud)
用法:
var selector = $(this).me();
var $twins = $(selector);
Run Code Online (Sandbox Code Playgroud)
接受根元素作为参数的替代版本:
$.fn.me = function(root){
return this.first().parentsUntil(root).andSelf().map(function(){
return this.tagName;
}).get().join(">");
};
Run Code Online (Sandbox Code Playgroud)
用法:
var selector = $(this).me("#root");
var $twins = $(selector, "#root");
Run Code Online (Sandbox Code Playgroud)
第三个版本,用于保持根元素下最顶层元素的后代的相对位置.例如,它将返回DIV>DIV:nth-child(1)>SPAN:nth-child(2)而不是通用DIV>DIV>SPAN选择器.
$.fn.me = function(root){
return this.first().parentsUntil(root).andSelf().map(function(){
var eq = i ? ":nth-child(" + ($(this).index() + 1) + ")" : "";
return this.tagName + eq;
}).get().join(">");
};
Run Code Online (Sandbox Code Playgroud)
用法:
var selector = $(this).me("#root");
var $twins = $(selector, "#root");
Run Code Online (Sandbox Code Playgroud)