[英]javascript dollar sign variable not working
我在Wordpress中有以下代碼:
(function ($) {
var $header = $("div.header");
$(window).bind("scroll resize", function () {
if ($(window).scrollTop() > 30) {
$("div.header").stop().animate({
'opacity': 0.24
}, {
duration: 1000
});
} else {
$header.stop().animate({
'opacity': 1
}, {
duration: 1000
});
}
});
})(jQuery);
如果聲明在假設時開始,但從來沒有......
但
如果我附上:
jQuery(document).ready(function($) {
// code here
});
一切都很好。 為什么?
謝謝
可能是你在嘗試使用jQuery而不是構建dom。 嘗試使用$(document).ready
函數:
(function ($) {
$(document).ready(function () {
$header = $("div.header");
$header.remove();
});
})(jQuery);
關於你在問題中提到的內容:
jQuery(document).ready(function ($) {
// code
});
它的工作原理是因為它做同樣的事情:它在ready
事件上綁定事件處理程序,並將jQuery
對象作為參數傳遞給函數$
。
現在你做了什么:
(function ($) {
$header = $("div.header");
$header.remove();
})(jQuery);
在這里,您只需使用命名的$
parameter聲明匿名函數:
function ($) {
}
並使用jQuery
對象作為參數調用它,該函數將在$
函數中可用:
(function ($) {
})(jQuery);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.