jquery 核心函数
$(function () {
$('#btn').click(function(){
// alert(this.innerHTML)
alert($(this).html())
// 创建input
$('<input type="text" name="msg2"/>').appendTo('.box')
})
})
// 数组遍历
var arr = [4,7,5,1];
$.each(arr,function(index,item){
console.log(index, item);
})
// 去掉 ' heoll word ' 两端空格
var str = ' heoll word ';
// 原生写法
console.log('---'+str+'---');
// console.log(str.trim().length);
console.log('---'+$.trim(str)+'---');
var $btns = $('button')
// <!--需求1. 统计一共有多少个按钮-->
// console.log($btns.length,$btns.size());
// <!--需求2. 取出第2个button的文本-->
// console.log($($btns[1]).html()); // 测试二
// console.log($btns[1].innerHTML); // 测试二
// <!--需求3. 输出所有button标签的文本-->
// $btns.each(function(index, item){
// console.log($(item).html());
// })
// $btns.each(function(index, item){
// console.log(this.innerHTML);
// })
// $btns.each((index, item)=>{
// console.log($(item).html());
// })
// <!--需求4. 输出'测试三'按钮是所有按钮中的第几个-->
$btns.each(function(index, item){
if(this.innerHTML == '测试三'){
console.log(index);
}
})
// 返回索引值 index 查找的是兄弟元素当中排第几
// console.log($('#btn3').index());
// 返回的是索引值 index 查找的是兄弟元素当中排第几
console.log($('#btn3').index());
我是Eric,手机号是13522679763

浙公网安备 33010602011771号