Jquery 选择器没有拉出文本
Jquery selector not pulling text out
我在 opencart 的框架内工作。基本安装具有完全相同的代码,但我的似乎不起作用。我没有看到任何错误。
我不确定如何让 jquery 中的选择器正常工作。当下面的代码是 运行 时,它在 span 中找不到文本。它返回一个空的警告框。我做错了什么或遗漏了什么吗?
alert($('#cart-total').text());
<button type="button"><span id="cart-total" ><?php echo $text_items; ?></span></button>
functions.min.js -> 第 197 行
如果您想实际查看它:http://oc1.theaamgroup.com/
按添加到购物车并观看。
更新: 我添加了一个新行来帮助调试过程。当您按下添加到购物车时,会弹出一个警告框,告诉您为#cart-total 找到的 jquery 选择器是什么,然后它应该找到什么。
我尝试过的事情:
正在更改 jquery 版本
将函数文件移动到其他文件之前加载
(选择器在某些情况下不在函数文件中时起作用
原因。)
functions.min.js 文件顶部的相同选择器
functions.min.js 文件底部的相同选择器
您可以在 chrome 控制台中 运行 命令,它工作正常。
多个 css 选择器。
更新 2
在 functions.min.js 第 498 行的末尾,我添加了 alert($('#cart-total').text());表明无论出于何种原因,即使在启动时 cart-total 也没有返回任何内容(默认情况下,如果您的购物车中没有产品)0 件 - $0.00
只有您在问题中提到的脚本对我有用。但更进一步,我检查了你链接到的页面,发现你有一个看起来像这样的函数:
if (json['success']) {
$('#content').parent().before('<div class="alert alert-success"><i class="fa fa-check-circle"></i> ' + json['success'] + '<button type="button" class="close" data-dismiss="alert">×</button></div>');
$('#cart-total').html(json['total']);
alert($('#cart-total').text());
$('html, body').animate({
scrollTop: 0
}, 'slow');
$('#cart > ul').load('index.php?route=common/cart/info ul li');
}
您正在更改 #cart-total
的 html,如果您已经用变量设置了文本,为什么还要再次获取文本?只需执行以下操作并将 alert($('#cart-total').text());
更改为 alert(json['total']);
像这样:
if (json['success']) {
$('#content').parent().before('<div class="alert alert-success"><i class="fa fa-check-circle"></i> ' + json['success'] + '<button type="button" class="close" data-dismiss="alert">×</button></div>');
$('#cart-total').html(json['total']);
alert(json['total']);
$('html, body').animate({
scrollTop: 0
}, 'slow');
$('#cart > ul').load('index.php?route=common/cart/info ul li');
}
我还不能评论...所以这不是一个完整的答案,但我建议 OP 像这样在 JSfiddle 中抽象出问题
http://jsfiddle.net/31c2zb0u/1/
此外,所有这些丑陋的标记都不是问题所必需的,并且会分散注意力。
<button type="button" onClick="cart.add('1274');"><i class="fa fa-shopping-cart"></i> <span class="hidden-xs hidden-sm hidden-md">Add to Cart</span></button>
以上是需要的所有信息 html。
<button onClick="cart.add('1274');"><span style='display: none'>Add to Cart</span></button>
您需要等到购物车完全更新,而这要等到 .load 事件之后才会发生。尝试使用回调函数更新 functions.min.js 的第 205 行:
$('#cart > ul').load('index.php?route=common/cart/info ul li',function(){
alert('#cart-total inner text is: '+$('#cart-total').text()+'\n\n#cart-total inner text should be: '+json['total']);
});
这样,当您尝试获取 $('#cart-total').text(); 的值时,所有内容都已到位;
有关 .load 的更多信息可在此处找到:http://api.jquery.com/load/
您在请求成功后加载 #cart
,因此您可以在使用回调获取内容后更新文本
$('#cart > ul').load('index.php?route=common/cart/info ul li', function(){
$('#cart-total').text(json['total']);
});
我在 opencart 的框架内工作。基本安装具有完全相同的代码,但我的似乎不起作用。我没有看到任何错误。
我不确定如何让 jquery 中的选择器正常工作。当下面的代码是 运行 时,它在 span 中找不到文本。它返回一个空的警告框。我做错了什么或遗漏了什么吗?
alert($('#cart-total').text());
<button type="button"><span id="cart-total" ><?php echo $text_items; ?></span></button>
functions.min.js -> 第 197 行
如果您想实际查看它:http://oc1.theaamgroup.com/ 按添加到购物车并观看。
更新: 我添加了一个新行来帮助调试过程。当您按下添加到购物车时,会弹出一个警告框,告诉您为#cart-total 找到的 jquery 选择器是什么,然后它应该找到什么。
我尝试过的事情:
正在更改 jquery 版本
将函数文件移动到其他文件之前加载
(选择器在某些情况下不在函数文件中时起作用 原因。)
functions.min.js 文件顶部的相同选择器
functions.min.js 文件底部的相同选择器
您可以在 chrome 控制台中 运行 命令,它工作正常。
多个 css 选择器。
更新 2 在 functions.min.js 第 498 行的末尾,我添加了 alert($('#cart-total').text());表明无论出于何种原因,即使在启动时 cart-total 也没有返回任何内容(默认情况下,如果您的购物车中没有产品)0 件 - $0.00
只有您在问题中提到的脚本对我有用。但更进一步,我检查了你链接到的页面,发现你有一个看起来像这样的函数:
if (json['success']) {
$('#content').parent().before('<div class="alert alert-success"><i class="fa fa-check-circle"></i> ' + json['success'] + '<button type="button" class="close" data-dismiss="alert">×</button></div>');
$('#cart-total').html(json['total']);
alert($('#cart-total').text());
$('html, body').animate({
scrollTop: 0
}, 'slow');
$('#cart > ul').load('index.php?route=common/cart/info ul li');
}
您正在更改 #cart-total
的 html,如果您已经用变量设置了文本,为什么还要再次获取文本?只需执行以下操作并将 alert($('#cart-total').text());
更改为 alert(json['total']);
像这样:
if (json['success']) {
$('#content').parent().before('<div class="alert alert-success"><i class="fa fa-check-circle"></i> ' + json['success'] + '<button type="button" class="close" data-dismiss="alert">×</button></div>');
$('#cart-total').html(json['total']);
alert(json['total']);
$('html, body').animate({
scrollTop: 0
}, 'slow');
$('#cart > ul').load('index.php?route=common/cart/info ul li');
}
我还不能评论...所以这不是一个完整的答案,但我建议 OP 像这样在 JSfiddle 中抽象出问题
http://jsfiddle.net/31c2zb0u/1/
此外,所有这些丑陋的标记都不是问题所必需的,并且会分散注意力。
<button type="button" onClick="cart.add('1274');"><i class="fa fa-shopping-cart"></i> <span class="hidden-xs hidden-sm hidden-md">Add to Cart</span></button>
以上是需要的所有信息 html。
<button onClick="cart.add('1274');"><span style='display: none'>Add to Cart</span></button>
您需要等到购物车完全更新,而这要等到 .load 事件之后才会发生。尝试使用回调函数更新 functions.min.js 的第 205 行:
$('#cart > ul').load('index.php?route=common/cart/info ul li',function(){
alert('#cart-total inner text is: '+$('#cart-total').text()+'\n\n#cart-total inner text should be: '+json['total']);
});
这样,当您尝试获取 $('#cart-total').text(); 的值时,所有内容都已到位;
有关 .load 的更多信息可在此处找到:http://api.jquery.com/load/
您在请求成功后加载 #cart
,因此您可以在使用回调获取内容后更新文本
$('#cart > ul').load('index.php?route=common/cart/info ul li', function(){
$('#cart-total').text(json['total']);
});