html和text的区别 html用于获取第一个匹配元素的HTML内容或者文本内容:let h=$('#div').html(); console.log(h); text会把div集合内的文字都获取
let h=$('#div').text(); console.log(h); 设置所有匹配元素的HTML内容或者文本。html可以获取标签,text不可以 html()可以对HTML代码进行操作,类似于JS中的innerHTML nnerHTML和html()设置是都会把原来的替换
$('#div1').html('我是一名放假的学生')
$('#div1').text('我是一名放假的学生
')
text是不能识别标签的 /鼠标移动到div上显示样式 $('div').mouseover(function(){
$(this).addClass('divClass')
/* toggleClass鼠标移入,如果没有样式就添加样式 */
//$(this).toggleClass('divClass big')
// })
/* 鼠标离开div时样式消失 */
// $('div').mouseout(function(){
/* $(this).removeClass('divClass') */
/*也是toggleClass 鼠标移入,如果有样式就删除样式 */
//$(this).toggleClass('divClass big')
// })
/* hasClass()方法来判断是否包含指定的样式 */
function toggleFn(/* that */){
/* 现在显示的是用封装的方式,传了一个参数that的方法这时函数内的this记得改成that ,同时可以使用继承的方法,这样就不用传参了*/
if($(this).hasClass('divClass big')){
$(this).removeClass('divClass big')
}else{
$(this).addClass('divClass big')
}
}
$('div').mouseover(function(){
/* if($('div').hasClass('divClass big')){
$(this).removeClass('divClass big')
}else{
$(this).addClass('divClass big')
} */
toggleFn.call(this)
})
$('div').mouseout(function(){
/* if($('div').hasClass('divClass')){
$(this).removeClass('divClass big')
}else{
$(this).addClass('divClass big')
} */
toggleFn.apply(this)
})