我在獲取腳本在CMS內工作時遇到了一些問題。這是一個相對簡單的jQuery顯示/隱藏,工作正常,直到我需要在一個頁面上多次使用它。我試圖將其轉換爲使用每個DIV的個人ID,但現在它在Firebug中返回錯誤說JQuery - ID未定義。在函數中使用動態ID?
ID未定義。
jQuery的
$('.articleSlide').each(function() {
var current = $(this);
current.attr("box_h", current.height());
$(".articleSlide").css("height", "250px");
$(".showHide").html('<a href="#">More</a>');
$(".showHide a").attr("href", "javascript:void(0)");
$(".showHide a").click(function() { openSlider() })
});
function openSlider()
{
var open_height = $("#articleSlide_" + id).attr("box_h") + "px";
$("#articleSlide_" + id).animate({"height": open_height}, {duration: "slow" });
$(".showHide").html('<a href="#">More</a>');
$(".showHide a").click(function() { closeSlider() })
}
function closeSlider()
{
$("#articleSlide_" + id).animate({"height": "250px"}, {duration: "slow" });
$(".showHide").html('<a href="#">More</a>');
$(".showHide a").click(function() { openSlider() })
}
HTML
<div class="articleSlide" id="articleSlide_1">
<p>We work closely with clients to provide effective solutions for a wide variety of products and applications through brand creation and management, video and motion graphics, marketing and advertising, digital and editorial.</p>
<p>We understand markets and how to communicate in a multi-platform environment.We work closely with clients to provide effective solutions for a wide variety of products and applications through brand creation and management, video and motion graphics, marketing and advertising, digital and editorial.</p>
</div>
<div class="showHide">
編輯
OK,我想我誤解了ID位我包括在內。我基本上希望它能夠拿起每個顯示/隱藏div的個人ID,所以它只能打開那個。這是我目前劇本的作品,但如果你在頁面上有多個DIV它打開和關閉所有這些
$(".articleSlide").each(function() {
var current = $(this);
current.attr("box_h", current.height());
$(".articleSlide").css("height", "250px");
$(".showHide").html('<a href="#">More</a>');
$(".showHide a").attr("href", "javascript:void(0)");
$(".showHide a").click(function() { openSlider() })
});
function openSlider()
{
var open_height = $(".articleSlide").attr("box_h") + "px";
$(".articleSlide").animate({"height": open_height}, {duration: "slow" });
$(".showHide").html('<a href="#">More</a>');
$(".showHide a").click(function() { closeSlider() })
}
function closeSlider()
{
$(".articleSlide").animate({"height": "250px"}, {duration: "slow" });
$(".showHide").html('<a href="#">More</a>');
$(".showHide a").click(function() { openSlider() })
}
<a href="#">More</a>
</div>
其中是你的'openSlider'和'closeSlider'函數定義了'id'變量嗎? – 2011-03-28 15:12:15
我誤解了我以爲我的工作。我已經編輯它現在更清楚一點 – Yammi 2011-03-28 15:35:16