使用 jQuery 隐藏 Javascript 数组

Hiding a Javascript Array with jQuery

本文关键字:数组 Javascript 隐藏 jQuery 使用      更新时间:2023-09-26

现在我有一个看起来像这样的函数:

function hidearray() {
    $('#one, #two, #three').hide();
}

我想将这些 ID 放入 1 个数组中,看起来像

var myArray['one', 'two', 'three'];

然后将该数组隐藏在我的函数 hidearray(( 中。 我以为它会看起来像这样,但我想我走错了路(我也知道我忽略了下面语句中的 #(

$(.each(myArray)).hide();

解决方案可能很简单,所以感谢所有回复的人!

jQuery 选择器只是字符串,因此通过连接数组来创建字符串:

$('#' + myArray.join(', #')).hide();

小提琴

我会使用getElementById.假设支持Array.map

var myArray = ['one', 'two', 'three'];
$(myArray.map(document.getElementById, document)).hide();
for(var i = 0; i < myArray.length; i++) {
     $(myArray[i]).hide();
 }

jsFiddle Demo

使用 each ,将数组作为第一个参数传递,第二个参数将是处理当前项目的回调

var arr = [];
arr.push("#one");
arr.push("#two");
arr.push("#three");
$.each(arr,function(){
 $(""+this).hide();
});

隐藏数组??.我想你的意思是。如何隐藏元素,通过使用jquery将它们的ID放在数组中

你可以这样做。

array = ['#id_tiempo_inicio_0', '#id_tiempo_fin_0', '#id_evento']
$.each(array,function(i, v){
   $(v).hide();
});
var myArray['one', 'two', 'three'];
$.each(myArray,function(i,v){
   $('#'+v).hide();
});

演示--> http://jsfiddle.net/Utg7p/

这将解决问题,

.html

<div id="one">One</div>
<div id="two">Two</div>
<div id="three">Three</div>
<button id="button">Hide</button>

JavaScript

jQuery(document).ready(function() {
    var mrHide = ['#one', "#two", "#three"];
    jQuery("button#button").click(function() {
        jQuery.each(mrHide, function(index, value) {
            jQuery(value).hide();
        });
    });
});

http://jsfiddle.net/maxja/LUf4y/2/