這裏是我的代碼的第一部分:傳遞變量在jQuery CSS選擇器不工作
$('ul li').click(function(){ //when an <li> is clicked
$('ul .clicked').removeClass('clicked'); // remove .clicked class from any other <li>'s inside a <ul>
$(this).addClass('clicked'); // add .clicked class to the clicked <li> ($(this))
screen = $(this).attr('id'); // screen = the clicked elements' id
screen = "#" + screen; // screen = the clicked elements' id with a # infront of it
$(screen).screenSlide(); // is basically = $('#elementsID').screenSlide();, correct?
});
這很奇怪,因爲在前面的功能,我寫的,我做了同樣的事情,除了最後一步,而不是作爲一個選擇器傳遞屏幕,我推動屏幕內的一個數組,然後我抓住數組[0](這是#elementsID沒有任何引用),並將其用作選擇器,它的工作。但前進,screenSlide是
function screenSlide(){ // $(this) should = an <li> who's id is screen
var test = $(this).attr('class');
alert(test);
$(this).addClass('current'); // add the .current class to $(this), which should be an <li>
$(this).slideDown();
};
現在,警報測試沒有提醒任何東西,所以我猜測,作爲CSS選擇器傳遞屏幕沒有工作。正如你所看到的,screenSlide函數應該爲$(this)添加一個類,然後使其滑動。
有什麼想法嗎?
你的意思是該警示框顯示了'undefined'呢,還是顯示不出來呢?另外,控制檯中是否有任何錯誤? –
爲什麼不直接調用'screenSlide(this);'? –
@SuperScript,當我這樣做時,警告框顯示未定義。 – user2817200