简体   繁体   English

使用 Javascript 从数组中提取值

[英]Extracting values from Array with Javascript

I am having issues with getting exactly values with Javascript.我在使用 Javascript 获取准确值时遇到问题。

Following is working version of when we have class on single item.以下是我们在单个项目上上课时的工作版本。 http://jsfiddle.net/rtnNd/ 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.实际上,当代码块具有更多具有相同类的项目时(请参阅: http : //jsfiddle.net/rtnNd/3 ),它仅选取使用类名称的第一个项目。

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.可能包含项目的代码正在使用相同的类,类可能在 2 个项目、3 个项目或第 6 次中使用。 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:好的,所以一般来说你会使用.last()方法:

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.除了那个类的 DOM 中没有元素,因为(在你的小提琴中)它们都被注释掉了。 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 . ...将item变量设置为undefined The selector '.itemAnchor' returns no elements, so $('.itemAnchor') is an empty jQuery object and it has no element at index 6 .选择器'.itemAnchor'返回任何元素,因此$('.itemAnchor')是一个空的 jQuery 对象,它在索引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:在使用.replace()语句删除开始和结束注释,您需要在 html 上使用'.itemAnchor'选择器,因此:

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

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

EDIT in response to comment:编辑以回应评论:

"How can we pick up the itemElement before that last one." “我们怎么能在最后一个之前拿起 itemElement。”

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/如果你知道你总是想要倒数第二个项目使用.slice(-2,-1)而不是.last() ,如下所示: 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() :或者,如果你知道你想要任何一个包含参数h=href ,那么你可以使用像'.itemAnchor[href*="h="]'.find()这样的选择器,在这种情况下你不需要.last().slice()

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

Demo: http://jsfiddle.net/rtnNd/6/演示: 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.请注意,尽管使用属性包含选择器的最后一种方法是选择href任何地方具有文本“h=”的元素因此它适用于您的情况,但也会选择hh=somethingmath=easy或其他任何内容。 You could avoid this and test for just h= as follows:您可以避免这种情况并仅测试h=如下:

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/演示: http : //jsfiddle.net/rtnNd/7/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM