我買了$('#my_id1')和document.getElementById('my_id1')是一樣的。但它是父母不是。有什麼不同?
(function($) {
$.fn.simple_hide_function = function() {
var $t = this;
$t.hide();
};
})(jQuery);
$(window).load(function() {
var $div1 = $('#my_id1');
var $div2 = document.getElementById('my_id2');
$div1.simple_hide_function(); // this is working
$div2.simple_hide_function(); // but this is not working
});
添加例如使其更清楚:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<div id="my_id1" style="height:100px;background:#f00">div1</div>
<div id="my_id2" style="height:100px;background:#f00">div2</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
(function($) {
$.fn.simple_hide_function = function() {
var $t = this;
$t.hide();
};
})(jQuery);
$(window).load(function() {
var $div1 = $('#my_id1');
var $div2 = document.getElementById('my_id2');
$div1.simple_hide_function();
$div2.simple_hide_function();
});
</script>
</body>
</html>
你有my_id1 vs my_id2 - 標識符不一樣 – Nobita 2011-12-29 11:39:53
你有div與「my_id2」ID ..! – 2011-12-29 11:40:19
用$開始你的變量名有點奇怪 - $ t,$ div1等等 - 僅僅因爲jQuery在任何地方使用$並不意味着你必須這樣做。哦,你認爲它的PHP? – Spacedman 2011-12-29 11:46:52