数组的ID - 如何用JavaScript / JQuery选择? [英] Array of IDs - how to select with JavaScript / JQuery?

查看:95
本文介绍了数组的ID - 如何用JavaScript / JQuery选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个元素标识符数组,从一些服务器端验证返回。 ID不带前缀#。

解决方案

p>不要忘记老式的getElementById - 它不需要哈希的id。然后只需将jQuery的节点添加到jQuery对象中:

  var ids = ['jq-primarySearch','jq-n -CSS']; 
var nodes = $ .map(ids,function(i){return document.getElementById(i)});
var jqObj = $(nodes);


I have an array of element identifiers, coming back from some server-side validation. The IDs aren't prefixed with a '#'. Rather than going through the array and prefixing a # to each member, is there jquery means of directly selecting all elements by their IDs?

解决方案

Don't you forget "old fashioned" getElementById - it doesn't require hashing the ids. Then just feed nodes to jQuery to get a jQuery object:

var ids = ['jq-primarySearch', 'jq-n-CSS'];
var nodes = $.map( ids, function(i) { return document.getElementById(i) } );
var jqObj = $(nodes);

这篇关于数组的ID - 如何用JavaScript / JQuery选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
HTML/CSS最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆