我在產品銷售網站上獲得了以下代碼。將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