Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.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/3/html/89.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中访问和更改具有给定字符串ID的div的html_Jquery_Html_Each - Fatal编程技术网

在jQuery中访问和更改具有给定字符串ID的div的html

在jQuery中访问和更改具有给定字符串ID的div的html,jquery,html,each,Jquery,Html,Each,我使用以下代码访问所有具有给定字符串ID的div(以字符串“brand”开头的div): 它工作得很好,并且一次隐藏了所有的div。但是我想得到一个所有受影响id的列表(数组)来更改它的.html(),但不知道如何实现。我已经尝试过了 jQuery('div[id ^= "' + tipo + '"]').each( alert(this.id); ); 要为找到的每个ID显示警报,但未成功。我应如何继续?您应将匿名函数传递给方法: 您应该将匿名函数传递给方法: 您需要提供一个回

我使用以下代码访问所有具有给定字符串ID的div(以字符串“brand”开头的div):

它工作得很好,并且一次隐藏了所有的div。但是我想得到一个所有受影响id的列表(数组)来更改它的.html(),但不知道如何实现。我已经尝试过了

 jQuery('div[id ^= "' + tipo + '"]').each(
     alert(this.id);
 );

要为找到的每个ID显示警报,但未成功。我应如何继续?

您应将匿名函数传递给方法:


您应该将匿名函数传递给方法:


您需要提供一个回调函数作为参数

jQuery('div[id^="' + tipo + '"]').each(function(){
    alert(this.id);
});
var tipo=“a”;
jQuery('div[id^=“”+tipo+'“]”)。每个(函数(){
警报(this.id);
});

您需要提供一个回调函数作为参数

jQuery('div[id^="' + tipo + '"]').each(function(){
    alert(this.id);
});
var tipo=“a”;
jQuery('div[id^=“”+tipo+'“]”)。每个(函数(){
警报(this.id);
});

您可以使用

var ids = jQuery('div[id ^= "' + tipo + '"]').map(function(){
     return this.id;
}).get();

注意使用
function
关键字创建一个匿名函数,该函数由
map
调用(称为回调)。

您可以使用

var ids = jQuery('div[id ^= "' + tipo + '"]').map(function(){
     return this.id;
}).get();
注意使用
function
关键字创建一个匿名函数,该函数由
map
调用(称为回调)。

试试这个

 jQuery('div[id ^= "' + tipo + '"]').each(function(){
     alert( $(this).attr('id') );
 )};
试试这个

 jQuery('div[id ^= "' + tipo + '"]').each(function(){
     alert( $(this).attr('id') );
 )};