我在 jQuery .each() 函数中缺少什么?
What am I missing in the jQuery .each() function?
我有这个功能,我正在尝试计算 out/fix,但似乎无法查明问题所在/无法找到让它工作的方法。
基本上我的 CMS 会吐出我想要的某些 hrefs:
第 1 部分 更改目标 href URL
第 2 部分更改按钮的文本
现在我只有 2 个这种类型的按钮实例,所以这是我控制台中打印的内容:
第 1 部分 对于这部分,我得到了正确的 url,没有我想删除的字符。
第 2 部分 按钮文本的两个实例(查看全部),后跟第一个按钮的正确变量 btnParent,然后是第二个按钮,最后是 "Products".
我的问题是,我不知道如何:
第 1 部分 将剥离的 URL 作为 each 函数发送回其各自按钮的 href。
第 2 部分 让 each() 函数为每个实例打印出 "See All + BLAH + Products" 的新文本,然后将新文本附加到相应的按钮。
代码如下:
function viewMoreBtn() {
var btnMain = $("li:contains('See All')");
var btnText = $("li:contains('See All')").text();
var btnParent = $("li:contains('See All')").parent('ul').prev('li').text();
// PART 1 - STRIP LINK URL OF -_-// CHARACTERS
$.each(btnMain, function(i, v) {
v = $(this).find('a').attr('href').replace('-_-//', '');
console.log(v);
});
// PART 2 - ADD LABEL TO HTML TEXT OF BTN
$.each(btnMain, function(index, value) {
value = (btnText + btnParent + 'Products');
$(btnMain).text(value);
console.log(value);
});
}
viewMoreBtn();
谢谢。
jQuery 对象,因为 $(...)
的 return 已经有一个 each
方法。该元素作为 this
上下文传递。您可以进一步将其与 jQuery 结合使用,以对范围上下文中的对象进行操作。基本上,您拥有正确的代码,只是在错误的范围内。
第 1 部分
btnMain.each(function() {
var $li = $(this);
var $a = $li.find('a');
var desiredUrl = $a.attr('href').replace('-_-//', '');
$a.attr('href', desiredUrl);
});
第 2 部分
btnMain.each(function() {
var $li = $(this);
var btnText = $li.text();
varbtnParent = $li.parent('ul').prev('li').text();
value = (btnText + btnParent + 'Products');
console.log(value);
$li.find('a').text(value);
});
参见@Zequ 对返回的 btnMain 中 each() 函数迭代的回答。
这就是 $.each( obj, function( key, value ) 的工作原理:您迭代 btnMain,对于 $.each() 的每次迭代,该函数将迭代的索引分配给 i 和该索引处 btnMain 的值 v。
$.each(btnMain, function(i, v) {
//v = $(this).find('a').attr('href').replace('-_-//', '');
console.log(i); // I am the index of $.each() iterator
console.log(v); // I am the node from the btnMain array
// I don't know if this is right without seeing your HTML, but it seems like what you want
v.find('a').attr('href').replace('-_-//', '');
});
第二个 $.each() 遵循相同的模式。
如果我理解正确的话,你是在混淆你的变量。
$.each 是传递的 array/object 的每个元素的函数。它给你一个索引和元素,检查 the reference
在第 1 部分中,您将 v 定义为您想要的字符串,您根本没有更改元素,您需要这样的东西:
$.each(btnMain, function() {
// you're saying you got the correct URLs, so the only thing you need to do is to change the element afterwards
var element = $(this).find('a');
v = element.attr('href').replace('-_-//', '');
element.attr('href', v);
});`
您也可以使用 btnMain.each
而不是 $.each
在第 2 部分中,您正在将 value 变量(它实际上是您要迭代的元素)更改为您想要的字符串,然后您通过尝试更改 btnMain 的文本。这是错误的,据我了解,btnMain 是一个包含两个元素的数组,您无法更改它的文本。您应该更改元素的值(您正在调用值)。应该是这样的
$.each(btnMain, function(index, element){
// I think this is the time you want to define the btnParent, relative to the element
var btnParent = element.parent('ul').prev('li').text();
var value = (btnText + btnParent + 'Products');
element.text(value);
}
我认为这就是您所需要的。
您也可以将两个部分合二为一,因为两者都在 btnMain
上迭代
我有这个功能,我正在尝试计算 out/fix,但似乎无法查明问题所在/无法找到让它工作的方法。
基本上我的 CMS 会吐出我想要的某些 hrefs:
第 1 部分 更改目标 href URL
第 2 部分更改按钮的文本
现在我只有 2 个这种类型的按钮实例,所以这是我控制台中打印的内容:
第 1 部分 对于这部分,我得到了正确的 url,没有我想删除的字符。
第 2 部分 按钮文本的两个实例(查看全部),后跟第一个按钮的正确变量 btnParent,然后是第二个按钮,最后是 "Products".
我的问题是,我不知道如何:
第 1 部分 将剥离的 URL 作为 each 函数发送回其各自按钮的 href。
第 2 部分 让 each() 函数为每个实例打印出 "See All + BLAH + Products" 的新文本,然后将新文本附加到相应的按钮。
代码如下:
function viewMoreBtn() {
var btnMain = $("li:contains('See All')");
var btnText = $("li:contains('See All')").text();
var btnParent = $("li:contains('See All')").parent('ul').prev('li').text();
// PART 1 - STRIP LINK URL OF -_-// CHARACTERS
$.each(btnMain, function(i, v) {
v = $(this).find('a').attr('href').replace('-_-//', '');
console.log(v);
});
// PART 2 - ADD LABEL TO HTML TEXT OF BTN
$.each(btnMain, function(index, value) {
value = (btnText + btnParent + 'Products');
$(btnMain).text(value);
console.log(value);
});
}
viewMoreBtn();
谢谢。
jQuery 对象,因为 $(...)
的 return 已经有一个 each
方法。该元素作为 this
上下文传递。您可以进一步将其与 jQuery 结合使用,以对范围上下文中的对象进行操作。基本上,您拥有正确的代码,只是在错误的范围内。
第 1 部分
btnMain.each(function() {
var $li = $(this);
var $a = $li.find('a');
var desiredUrl = $a.attr('href').replace('-_-//', '');
$a.attr('href', desiredUrl);
});
第 2 部分
btnMain.each(function() {
var $li = $(this);
var btnText = $li.text();
varbtnParent = $li.parent('ul').prev('li').text();
value = (btnText + btnParent + 'Products');
console.log(value);
$li.find('a').text(value);
});
参见@Zequ 对返回的 btnMain 中 each() 函数迭代的回答。
这就是 $.each( obj, function( key, value ) 的工作原理:您迭代 btnMain,对于 $.each() 的每次迭代,该函数将迭代的索引分配给 i 和该索引处 btnMain 的值 v。
$.each(btnMain, function(i, v) {
//v = $(this).find('a').attr('href').replace('-_-//', '');
console.log(i); // I am the index of $.each() iterator
console.log(v); // I am the node from the btnMain array
// I don't know if this is right without seeing your HTML, but it seems like what you want
v.find('a').attr('href').replace('-_-//', '');
});
第二个 $.each() 遵循相同的模式。
如果我理解正确的话,你是在混淆你的变量。
$.each 是传递的 array/object 的每个元素的函数。它给你一个索引和元素,检查 the reference
在第 1 部分中,您将 v 定义为您想要的字符串,您根本没有更改元素,您需要这样的东西:
$.each(btnMain, function() {
// you're saying you got the correct URLs, so the only thing you need to do is to change the element afterwards
var element = $(this).find('a');
v = element.attr('href').replace('-_-//', '');
element.attr('href', v);
});`
您也可以使用 btnMain.each
而不是 $.each
在第 2 部分中,您正在将 value 变量(它实际上是您要迭代的元素)更改为您想要的字符串,然后您通过尝试更改 btnMain 的文本。这是错误的,据我了解,btnMain 是一个包含两个元素的数组,您无法更改它的文本。您应该更改元素的值(您正在调用值)。应该是这样的
$.each(btnMain, function(index, element){
// I think this is the time you want to define the btnParent, relative to the element
var btnParent = element.parent('ul').prev('li').text();
var value = (btnText + btnParent + 'Products');
element.text(value);
}
我认为这就是您所需要的。 您也可以将两个部分合二为一,因为两者都在 btnMain
上迭代