从阵列中提取值的JavaScript [英] Extracting values from Array with Javascript

查看:103
本文介绍了从阵列中提取值的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有准确获取值使用Javascript的问题。

以下是工作时我们对单个项目类的版本。
http://jsfiddle.net/rtnNd/

其实当code座有更多的项目进行同一类(见: http://jsfiddle.net / rtnNd / 3 ),它拿起仅仅使用类名的第一个项目。

我的问题是,我想拿起仅仅使用类名的最后一个项目。所以我用以下code:

  VAR项目= $('itemAnchor。')[6];
变种HREF = $($('hidden_​​elem。')[1] .innerHTML.replace('&下;! - ','').replace(' - >',''))。找到(产品) .attr('href属性);

不过,这并不虽然工作。我真的不知道为什么。

这可能包含项目,采用同一类的code,类可能在2项,3项或第6次使用。这就是为什么我要拿起唯一的最后一个项目来提取。

您能解释一下,谢谢大家阅读我的问题。


解决方案

  

我的问题是,我想拿起仅仅使用类名的最后一个项目。


OK,所以在一般意义上,你会使用。去年()方法:

  VAR lastItem = $('itemAnchor。')最后()。

除了有与该类因为(在你的小提琴),他们都注释掉DOM没有元素。这也是为什么code,你在你的问题表明没有工作的原因。第一行:

  VAR项目= $('itemAnchor。')[6];

...设置项目变量未定义。选择。itemAnchor返回任何元素,所以 $('。itemAnchor')是一个空的jQuery对象它在指数没有元素 6

您需要在的使用。itemAnchor'上,你得到的的HTML 选择删除开幕,并与你的<$收评C $ C> .replace()语句,因此:

  VAR的href = $($('hidden_​​elem。')[0] .innerHTML.replace('&LT;! - ','')取代(' - &GT ;',''))
          .find('itemAnchor。')最后一个()ATTR('href属性)。

演示: http://jsfiddle.net/rtnNd/4/

编辑回应评论:


  

我们怎么可以在最后一个前拿起itemElement。


如果你知道你总是希望第二最后一个项目使用 .slice(-2,-1)而不是的。去年(),如下所示: http://jsfiddle.net/rtnNd/5/

或者,如果你知道你想要哪一个具有包含参数的的href H = 则可以使用如选择。itemAnchor [HREF * =H =] .find(),其中如果你不需要。去年() .slice()

  VAR的href = $($('hidden_​​elem。')[0] .innerHTML.replace('&LT;! - ','')取代(' - &GT ;',''))
          .find('。itemAnchor [HREF * =H =]')ATTR('href属性)。

演示: http://jsfiddle.net/rtnNd/6/

不过,请注意使用属性包含选择是捡元素,其中的href 的文本H =的任何地方,的所以它适用于你的情况,但也拿起 HH =东西数学=易或什么的。你可以避免这种情况和测试只是 H = 如下:

  VAR的href = $($('hidden_​​elem。')[0] .innerHTML.replace('&LT;! - ','')取代(' - &GT ;',''))
    .find('。itemAnchor')
    .filter(函数(){
        返回/(\\?|&)h=/.test(this.href);
    })ATTR('href属性)。

演示: http://jsfiddle.net/rtnNd/7/

I am having issues with getting exactly values with Javascript.

Following is working version of when we have class on single item. http://jsfiddle.net/rtnNd/

Actually when code block has more items with same class (see this: http://jsfiddle.net/rtnNd/3), it picks up only the first item which use the class name.

My issue is that I would like to pick up only the last item which use the class name. So I used following code:

var item = $('.itemAnchor')[6]; 
var href = $($('.hidden_elem')[1].innerHTML.replace('<!--', '').replace('-->', '')).find(item).attr('href'); 

But it doesn't work though. I don't really know why.

The code that may contains items are using same class, class may be use in 2 items, 3 items, or 6th times. That's why, I want to pick up only the last item to extract.

Can you explain, thank you all for reading my question.

解决方案

"My issue is that I would like to pick up only the last item which use the class name."

OK, so in a general sense you would use the .last() method:

var lastItem = $('.itemAnchor').last();

Except that there are no elements in the DOM with that class because (in your fiddles) they're all commented out. This is also the reason why the code you showed in your question didn't work. The first line:

var item = $('.itemAnchor')[6];

...sets the item variable to undefined. The selector '.itemAnchor' returns no elements, so $('.itemAnchor') is an empty jQuery object and it has no element at index 6.

You need to use the '.itemAnchor' selector on the html that you get after removing the opening and closing comments with your .replace() statements, so:

var href = $($('.hidden_elem')[0].innerHTML.replace('<!--','').replace('-->',''))
          .find('.itemAnchor').last().attr('href');

Demo: http://jsfiddle.net/rtnNd/4/

EDIT in response to comment:

"How can we pick up the itemElement before that last one."

If you know you always want the second-last item use .slice(-2,-1) instead of .last(), as shown here: http://jsfiddle.net/rtnNd/5/

Or if you know you want whichever one has an href that contains a parameter h= then you can use a selector like '.itemAnchor[href*="h="]' with .find(), in which case you don't need .last() or .slice():

var href = $($('.hidden_elem')[0].innerHTML.replace('<!--','').replace('-->',''))
          .find('.itemAnchor[href*="h="]').attr('href');

Demo: http://jsfiddle.net/rtnNd/6/

Note though that this last method using the attribute-contains selector is picking up elements where the href has the text "h=" anywhere, so it works for your case but would also pick up hh=something or math=easy or whatever. You could avoid this and test for just h= as follows:

var href = $($('.hidden_elem')[0].innerHTML.replace('<!--','').replace('-->',''))
    .find('.itemAnchor')
    .filter(function() {
        return /(\?|&)h=/.test(this.href);
    }).attr('href');

Demo: http://jsfiddle.net/rtnNd/7/

这篇关于从阵列中提取值的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆