English 中文(简体)
• 采取更合理、更高效的方式,将过去连续6个多功能部分掌握在一行?
原标题:More elegant/efficient way to get the 6 consecutive previous DOM elements in a row?

假设我有一个JQuery物体,储存在$e上,通过这一选择获得:

var $e = $("input[type= text ][value= " + v + " ]");

<代码>v为$(this.val(),我可在.each(>内查阅,但实际确实如此,可以是任何数字数值。

我想创建一套包含以下内容的JQuery:$/code>,DOM要素随附后,还有6个DOM要素在之前。

The function itself works perfectly fine, I m using this code for each iteration:

var $e = $("input[type= text ][value= " + v + " ]");
$e = $e.add($e.next());
for (i=0;i<6;i++)
    $e = $e.add($e.prev());
$e.remove();

I guess it s a little bit hard to explain, so take a look at this JSFiddle.

我在受<代码>约束的匿名职能中摘录上述代码。 移除第2条纽扣吨,通过固定的<代码>v值,以备本人的印本。

I m astarter with JQuery,甚至在读到 我找不到更简单的方式这样做。 我认为,<代码>.siblings(),或更具体地说,。 如果我能够具体说明 j类物体阵列的范围,那么<(前几英美哥)就可以更简单地做到这一点。

紫外线是通过一个PHP模板生成的,而PHP模板是我避免编辑的,因此Ill接受的答复,其中不包括在集装箱/集装箱内包装特定元素。

是否有更简单的办法来挑选特定的JQuery挑选人以前的6个要素,而不在任何集装箱或包装内包装这些要素?

最佳回答

Try using .prevAll and slice the elements You need. 见下文。

rel=“nofollow>>DEMO

    var $prevAll = $e.prevAll();
    $e = $e.add($e.next()).add($prevAll.slice(0, 6));

<><>Edit>: 增加<代码>(e.add($.next()),以添加下一个要素。

问题回答

您可使用<代码>siblings(和slice():

var $e = $("input:text[value= " + v + " ]");    

$e.add( $e.siblings().slice(0, 5) ).add( $e.prev() );

我不知道它会做得更好,但又选择:

    var $siblings = $e.siblings().andSelf();
    var index = $siblings.index($e);

    $siblings.slice(index-6,index+2).remove();

http://jsfiddle.net/Lf3xm/5/>rel=“nofollow> http://jsfiddle.net/Lf3xm/5/





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签