我p在班上有一些标签PointsToggle
<p class="PointsToggle">POINTS STATEMENT - AVAILABLE 7AM TOMORROW</p>
<p class="PointsToggle">SOME OTHER TEXT</p>
Run Code Online (Sandbox Code Playgroud)
还有一些像这样的jQuery
$('.PointsToggle').each(function() {
if ($(this).text = 'POINTS STATEMENT - AVAILABLE 7AM TOMORROW') {
$(this).css('width', '510px');
}
else {
$(this).css('width', '20px');
}
})?
Run Code Online (Sandbox Code Playgroud)
但我似乎无法让它发挥作用
有任何想法吗?
谢谢
杰米
如何在每次迭代完成后正确执行函数f,这样我可以计算具有该特定类的元素?
这给出0而不是16;
f = check_hfouten();
$.each(rest, function(idx, val,f) {
//alert(idx + ': ' + val);
$('td.info.'+idx).children('span').addClass('fout').attr('title',val).end().parent('tr').find('input').addClass('overlayed').click(function(){$(this).removeClass('overlayed')});
$('.tag_cloud.'+idx).addClass('reminder');
}).function(f);
Run Code Online (Sandbox Code Playgroud)
谢谢理查德
我有这个例子:
var name;
var id;
var array = [];
$.each(data, function(index, element) {
name = element.name;
id = element.id;
array[id] = name;
<a href="#" onClick="myFunction(array)">send</a>
console.log(array);
});
Run Code Online (Sandbox Code Playgroud)
在这种情况下.each将迭代5次,id将成为1, 2, 3, 4, 5和name将变更为五个名字
我想创建一个多维数组或一个如下所示的对象:
[1:name1] for the first iteration
[2:name2] for the second on
...
Run Code Online (Sandbox Code Playgroud)
将每对值传递给myFunction函数,并在该函数内部访问数组值:
function myFunction(array){//提醒键和值}
任何想法我怎样才能完成这个场景?
我试图循环一个javascript对象,但我不能让它运行.数据来自localStorage.
我的输出:
{"widget": {"title": "blablabla", "color": "yellow"},"widget": {"title": "lorem ipsum", "color": "black"},......}
Run Code Online (Sandbox Code Playgroud)
//我尝试过的(关键作品)
var list = JSON.parse(the localStoragekey);
for(var key in list){
if (list.hasOwnProperty(key)){
console.log(list[key])
}
}
Run Code Online (Sandbox Code Playgroud)
我看过网络(和Stackoverflow),但我似乎无法找到一个有效的解决方案.
我想在找到匹配的关键字时为元素添加一个类href
HTML,
<ul class="menu-header">
<li><a href="http://localhost/website/#/home/" class="button-header current">HOME</a></li>
<li><a href="http://localhost/website/#/gallery/" class="button-header">GALLERY</a></li>
<li><a href="http://localhost/website/#/people/" class="button-header">PEOPLE</a></li>
<li><a href="http://localhost/website/#/contact/" class="button-header">Contact</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
因此,如果关键字是"gallery",则应将"current"添加到该元素,并且必须删除其他元素中的"current"类.
jQuery的,
var request = "gallery";
// Add the class to the menu.
$(".menu-header li").each(function(){
var object = $(this); // which is <li>
var siblings = object.siblings(); // other <li>s
// Clean all siblings' style.
siblings.find("a").removeClass('current');
var array_link = $("a",this).attr("href").split('/');
if ($.inArray(request, array_link) == 1) $("a.button-header",object).addClass("current");
});
Run Code Online (Sandbox Code Playgroud)
我设法从所有元素中删除"当前"类,但无法添加"当前"类.
我错过了什么想法?
我正在使用jQuery的$.each()方法迭代属于某个类(例如class="definition")成员的所有DOM元素,然后将每个元素的ID打印到控制台.但是,迭代似乎只适用于首先定义指定类的元素.因此,对于具有多个类的元素,如果指定的类未在HTML中首先列出,则它似乎被"跳过".
例如:
HTML
<p class="definition" class="snippet" id="P1">Paragraph 1</p>
<p class="open" class="definition" class="snippet" id="P2">Paragraph 2</p>
<p class="snippet" class="definition" id="P3">Paragraph 3</p>
<p class="definition" class="snippet" id="P4">Paragraph 4</p>
<p class="definition" id="P5">Paragraph 5</p>
Run Code Online (Sandbox Code Playgroud)
JavaScript的
$( document ).ready(function() {
$(".definition").each(function() {
console.log(this.id);
});
});
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,控制台记录P1,P4和P5的ID,但不记录其他ID.
是否有理由确定定义类的类型顺序应该确定jQuery是否$.each()按类识别元素?
当我尝试使用3个不同的URL获得3个不同的结果时,'each'函数返回3次undefined.
var src = $('img').each(function(){
$(this).attr("src");
alert(src);
});
Run Code Online (Sandbox Code Playgroud)
我在SO上找到了几个答案,但他们似乎都没有回答这个(非常基本的)问题.
我想要做的是用更具体的URL替换每个img url
例如:
a.jpg --> a-ok.jpg
b.jpg --> b-ok.jpg
c.jpg --> c-ok.jpg
Run Code Online (Sandbox Code Playgroud)
这就是为什么我需要使用EACH的所有img的URL.
请帮忙.
谢谢
我不明白jQuery.each函数的语义.这种代码的和平工作正常:
arr = $("#foo").find("div");
// modify css formatting for all sub-divs
$.each(arr, function(name, value) {
arr.eq(name).css({backgroundColor:'red', fontWeight:'bold'});
});
Run Code Online (Sandbox Code Playgroud)
我没有理解为什么我不能直接访问这样的值
value.css({backgroundColor:'red', fontWeight:'bold'});
Run Code Online (Sandbox Code Playgroud)
因为据我所知(键,值)对,值应该等于arr [name].
我试图给每个.scan一个新的课程,但随着计数的增加
我试过的是使用jQuery中的每个函数然后使用$(this)但是$(this)将始终仅包含类名,因此每个.scan都将被更改.
$( ".scan" ).each(function() {
i = 0;
$(this).addClass( "count"+i );
i++;
});
Run Code Online (Sandbox Code Playgroud)
结果是
<div class="scan count0"></div>
<div class="scan count0"></div>
Run Code Online (Sandbox Code Playgroud)
但我想要的是
<div class="scan count0"></div>
<div class="scan count1"></div>
Run Code Online (Sandbox Code Playgroud)
这是我当前状态的小提琴.
每个tipp /帮助都非常感谢.
我想在一个内部触发一个函数each loop.这个问题来自于$(this)这之外的each loop
function look () {
$(this).css("color","blue");
};
$(".text").each(function(){
look();
// other functions()
//...
});
Run Code Online (Sandbox Code Playgroud)
我知道我可以把它放在$(this)里面each loop,但是我想这样做,因为我有很多小功能要放置.我能怎么做 ?谢谢.尼古拉斯.