我在产品销售网站上获得了以下代码。将ID /类转换为字符串
<html>
<body>
<div class="descr">
<div id="productName"></div>
</div>
</body>
</html>
#productName
是唯一的每个产品(即blackTea,whiteTea,绿茶,等...)
我插入一个div id为#productName_description
内#productName
。我需要一个脚本来抓取descr:first-child
的ID,并将"_description"
添加到最后,并将其应用于我尝试加载的内容。 Essenssially我想要做的是有一个自动化的脚本,将做到这一切没有我不必每写产品额外的javascript:
$('#kyoto_matcha').load('https://www.assamteacompany.ca/skin/assam_tea_company/script/product_descriptions.html #kyoto_matcha_description');
这是我得最远。花了几个小时在网上看到这个问题后,或试图找出解决方案,我无法弄清楚。当通过console.log()输出时,我不断收到[object Object] _description。这是我所得到的最接近:
$('.descr:first-child').each(function(){
var teaAccessory = $('.descr:first-child').attr('id');
var description = '_description';
$(this).load('https://www.assamteacompany.ca/skin/assam_tea_company/script/product_descriptions.html #' + teaAccessory + description);
});
这个问题是发布代码的外部问题,因为attr('id')* *绝对*返回一个字符串,而不是一个对象。您关于“将ID /类转换为字符串”的问题无法回答,因为它们*已经是*字符串。 – meagar 2012-02-20 19:30:08
看到这个:http://jsfiddle.net/QfDv9/ – 2012-02-20 19:31:35
好吧,所以product_descriptions.html包含所有的产品描述,你试图提取它们并将它们插入到你的新文档? – Dave 2012-02-20 19:32:05