我正在学习jQuery,并且是学习jQuery第4章的第一个例子。我有2个查询。 1)在检查点击按钮的ID时我们使用的是this.id而不是$(this).id,通常我们总是使用$符号但不在此。请解释一下。
2)在使用$ speech时,我尝试使用$(语音).css并且它不起作用。请告诉我原因。
以下是我的代码。
<html lang="en">
<head>
<title>Page 87</title>
<link rel="stylesheet" href="04.css" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
var $speech=$("div.speech");
$("#switcher-default").click(function(){
$("#switcher button").removeClass("selected");
$(this).addClass("selected");
$speech.css('fontSize','1em');
});
$("#switcher button").click(function(){
$("#switcher button").removeClass("selected");
$(this).addClass("selected");
var num=parseFloat($speech.css('fontSize'));
if(this.id=='switcher-large')
{
num*=1.4;
}
else if(this.id=='switcher-small')
{
num/=1.4;
}
$speech.css('fontSize',num+'px');
});
});
</script>
</head>
<body>
<div id="container">
<h2>Abraham Lincoln's Gettysburg Address</h2>
<div id="switcher">
<div class="label">Text Size</div>
<button id="switcher-default">Default</button>
<button id="switcher-large">Bigger</button>
<button id="switcher-small">Smaller</button>
</div>
<div class="speech">
<p>Fourscore and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.</p>
<p>Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battlefield of that war. We have come to dedicate a portion of that field as a final resting-place for those who here gave their lives that the nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we cannot dedicate, we cannot consecrate, we cannot hallow, this ground.</p>
<a href="#" class="more">read more</a>
<p>The brave men, living and dead, who struggled here have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember, what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced.</p>
<p>It is rather for us to be here dedicated to the great task remaining before us—that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion—that we here highly resolve that these dead shall not have died in vain—that this nation, under God, shall have a new birth of freedom and that government of the people, by the people, for the people, shall not perish from the earth.</p>
</div>
</div>
</body>
</html>
答案 0 :(得分:0)
this
是对作为事件源的html DOM元素的引用。
$(this)是围绕该元素的jQuery包装器,它允许使用jQuery方法。
$(this).attr('id')
和this.id
相同
但如果元素没有ID,则this.id
将返回空字符串
$(this).attr('id')
将返回undefined
$(this).id
,//这是错误的语法,,,实际上就像$(this).attr('id')
this
用于获取您在id
之前使用过的class
或event
。
喜欢
$('#five').click(function()
{
$(this).height();// here this means #five(same id you clicked )
}
和var $ speech = $(“div.speech”);
$speech.css('fontSize','1em'); //this is correct syntax.
it is same as $("div.speech").css('fontSize','1em');
$(speech).css //this is wrong
答案 1 :(得分:0)
根据变量的内容使用$(name)或名称。
var test = $('body');
test.css('background', 'red'); // GOOD - 'test' is the jQuery object variable
$(test).css('background', 'red'); // GOOD
var test = $('body')[0]; //
test.css('background', 'red'); // BAD - 'test' is the DOM object that not have css proto
$(test).css('background', 'red'); // GOOD - we can change DOM object to jQuery object
但在您的示例中,我们有误导性的变量名称:
var $speech=$("body");
$($speech).css('background', 'red');