Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jQuery隐藏Javascript数组_Javascript_Jquery_Arrays - Fatal编程技术网

使用jQuery隐藏Javascript数组

使用jQuery隐藏Javascript数组,javascript,jquery,arrays,Javascript,Jquery,Arrays,现在我有一个函数如下所示: function hidearray() { $('#one, #two, #three').hide(); } 我想把这些ID放到一个数组中 var myArray['one', 'two', 'three']; 然后将该数组隐藏在我的函数hiderray中。我原以为会是这样的,但我想我走错了方向我也知道我忽略了下面声明中的 $(.each(myArray)).hide(); 解决方案可能很简单,因此感谢所有回复 我会使用getElementById。

现在我有一个函数如下所示:

function hidearray() {
    $('#one, #two, #three').hide();
}
我想把这些ID放到一个数组中

var myArray['one', 'two', 'three'];
然后将该数组隐藏在我的函数hiderray中。我原以为会是这样的,但我想我走错了方向我也知道我忽略了下面声明中的

$(.each(myArray)).hide();
解决方案可能很简单,因此感谢所有回复

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

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

演示->


演示->

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

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

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

$('#' + myArray.join(', #')).hide();
使用each,将数组作为第一个参数传递,第二个参数将是处理当前项的回调

var arr = [];
arr.push("#one");
arr.push("#two");
arr.push("#three");
$.each(arr,function(){
 $(""+this).hide();
});
使用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();
});
隐藏数组??。 我想你是说。如何通过使用jquery将元素的id放入数组中来隐藏元素

你可以这样做

array = ['#id_tiempo_inicio_0', '#id_tiempo_fin_0', '#id_evento']

$.each(array,function(i, v){
   $(v).hide();
});

这样就行了

html


这样就行了

html


@minitech-确保字符串是字符串非常重要。@minitech-因为在其他情况下它不会被当作字符串对待。@TravisJ:哦,对了。jQuery及其愚蠢的字符串对象不是字符串,正确的修复方法是使用内置参数->我个人更喜欢。不,实际上不要那样做@minitech-确保字符串是字符串非常重要。@minitech-因为在其他情况下它不会被当作字符串对待。@TravisJ:哦,对了。jQuery及其愚蠢的字符串对象不是字符串,正确的修复方法是使用内置参数->我个人更喜欢。不,实际上不要那样做+我认为。加入是最直接的方法。谢谢大家这么快的评论!这是最短的解决方案。+1我认为。加入是最简单的方法。感谢大家这么快的评论!这是最短的解决方案。
<div id="one">One</div>
<div id="two">Two</div>
<div id="three">Three</div>
<button id="button">Hide</button>
jQuery(document).ready(function() {
    var mrHide = ['#one', "#two", "#three"];
    jQuery("button#button").click(function() {
        jQuery.each(mrHide, function(index, value) {
            jQuery(value).hide();
        });
    });
});