子元素可以为:stirng | element(js对象) | jquery元素
/* append 和 appendTo : 追加到某个元素之后 */ //父元素.append(子元素) $('ul').append('<li>抽象类1</li>') // jq对象后,末尾添加 子对象 //子元素.appendTo(父元素) $('<li>抽象类2</li>').appendTo('ul') // 子对象必须转成jq对象 ,末尾添加 Jq对象后 /* prepend 和 prependTo : 添加到某个元素之前 */ //父元素.prepend(子元素) $('ul').prepend('<li>抽象类1</li>') // jq对象前,添加 子对象 //子元素.prependTo(父元素) $('<li>抽象类2</li>').prependTo('ul') // 子对象必须转成jq对象 ,添加 Jq对象前 /* after 和 insertAfter : 兄弟元素之后插入某个元素/内容 */ //兄弟元素.after(要插入的兄弟元素) $('#l2').after('<li>哈哈</li>') //要插入的兄弟元素.insertAfter(兄弟元素) $('<li>哈哈2</li>').insertAfter('#l2') /* before 和 insertBefore : 兄弟元素之前插入某个元素/内容 */ //兄弟元素.before(要插入的兄弟元素) $('#l2').before('<li>哈哈3</li>') //要插入的兄弟元素.insertBefore(兄弟元素) $('<li>哈哈4</li>').insertBefore('#l2') //PS: 如果追加的jquery对象原本在文档树中,那么这些元素将从原位置上消失。简言之,就是移动操作。 示例代码:?
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="../day49 JQ/jquery3.4.1.js"></script> </head> <body> <ul> <li>唱</li> <li id="l2">跳</li> <li>rap</li> </ul> </body> <script> var li=document.createElement('li') // console.log($('ul').text()) // 查询所有文本内容 // console.log($('ul').html()) // 查询所有文本内容+标签 $('ul').append('<li>抽象类1</li>') // jq对象后,末尾添加 子对象 $('<li>抽象类2</li>').appendTo('ul') // 子对象必须转成jq对象 ,末尾添加 Jq对象后 // $('ul').prepend('<li>抽象类1</li>') // jq对象前,添加 子对象 $('<li>抽象类2</li>').prependTo('ul') // 子对象必须转成jq对象 ,添加 Jq对象前 $('#l2').after('<li>哈哈</li>') // $('<li>哈哈2</li>').insertAfter('#l2') // $('#l2').before('<li>哈哈3</li>') // $('<li>哈哈4</li>').insertBefore('#l2') // </script> </html> 示例代码:?
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="jquery3.4.1.js"></script> </head> <body> <button>按钮</button> </body> <script> $('button').click(function () { alert('123') }) </script> </html> 示例代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="jquery3.4.1.js"></script> </head> <body> <ul> <li >一</li> <li >二</li> <li >三</li> </ul> </body> <script> //创建一个sp对象 var sp=document.createElement('span') sp.innerText='alex' // replaceWith $('li:first').replaceWith(sp); //replaceAll $(sp).replaceAll($('li:last')) </script> </html> 示例代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="jquery3.4.1.js"></script> </head> <body> <button>点击</button> </body> <script> $('button').click(function () { // this 指的是当前的这个对象 var new_btn= $(this).clone() $(new_btn).insertAfter($(this)) }) // 当 clone(true) 克隆这个标签的所有,包括事件 $('button').click(function () { // this 指的是当前的 js对象 var new_btn= $(this).clone(true) //insertAfter 添加到button标签之后 $(new_btn).insertAfter($(this)) }) </script> </html> 综合示例代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="jquery3.4.1.js"></script> </head> <body> <!--<div title="abc" id="123"></div>--> <input type="radio" name="sex" value="1" checked>男 <input type="radio" name="sex" value="2">女 <input type="checkbox" name="hobby" value="1" >唱 <input type="checkbox" name="hobby" value="2" >跳 <input type="checkbox" name="hobby" value="3" >rap </body> <script> var div=$('div'); //获得属性值 var v1=div.attr('id') console.log(v1) //设置属性值 div.attr('name','ab');//单个 div.attr({'name':'ab','value':'123'});//多个 // 移除属性 div.removeAttr('id');//移除一个属性 div.removeAttr('id title') ; // 移除多个属性 //prop 查看属性 console.log(div.prop('id')) //prop 修改属性,属性必须已经存在标签上 // 一个值 $('div').prop('name','1bsad') // 多个值 $('div').prop({'id':'1bsad','title':'你好啊'}) </script> </html> 综合示例代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <script type="/javascript" src="jquery-3.3.1.js"></script> <style type="text/css"> div{ width: 100px; height: 100px; } .active { background-color: red; } .c1{ width: 200px; background-color: cornflowerblue; } .c2{ background-color: gray; } </style> <script src="jquery3.4.1.js"></script> </head> <body> <div class=""></div> <button>点击</button> </body> <script type="text/javascript"> $('button').click(function () { // $('div').addClass('c1') ; // 添加一个样式 // $('div').addClass('c1 c2'); //添加多个样式 // $('div').removeClass('c1') // 移除指定样式 // $('div').removeClass() // 移除所有样式 // 点击切换 $('div').toggleClass('c2') }) </script> </html> 示例代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <form action=""> <input type="radio" name="sex" value="112" />男 <!-- 设置cheked属性表示选中当前选项 --> <input type="radio" name="sex" value="11" checked="" />女 <input type="radio" name="sex" value="11" />gay <input type="checkbox" value="a" checked=""/>吃饭 <input type="checkbox" value="b" />睡觉 <input type="checkbox" value="c" checked=""/>打豆豆 <!-- 下拉列表 option标签内设置selected属性 表示选中当前 --> <select name="timespan" id="timespan" class="Wdate" > <option value="1">8:00-8:30</option> <option value="2" selected="">8:30-9:00</option> <option value="3">9:00-9:30</option> </select> <input type="text" name="" id="" value="111" /> </form> </body> <script type="text/javascript" src="jquery3.4.1.js"></script> <script type="text/javascript"> $(function(){ // 一、获取值 //1.获取单选框被选中的value值 console.log($('input[type=radio]:checked').val()) //2.复选框被选中的value,获取的是第一个被选中的值 console.log($('input[type=checkbox]:checked').val()) //3.下拉列表被选中的值 var obj = $("#timespan option:selected"); // 获取被选中的值 var time = obj.val(); console.log(time); // 获取文本 var time_text = obj.text(); console.log("val:"+time+" text"+ time_text ); //4.获取文本框的value值 console.log($("input[type=text]").val())//获取文本框中的值 // 二.设置值 //1.设置单选按钮和多选按钮被选中项 $('input[type=radio]').val(['112']); $('input[type=checkbox]').val(['a','b']); //2.设置下拉列表框的选中值,必须使用select /*因为option只能设置单个值,当给select标签设置multiple。 那么我们设置多个值,就没有办法了,但是使用select设置单个值和多个值都可以 */ $('select').val(['3','2']) //3.设置文本框的value值 $('input[type=text]').val('试试就试试') }) </script> </html> 示例代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="jquery3.4.1.js"></script> <style> div{ width: 100px; height: 100px; } </style> </head> <body> <div></div> <input type="text" name="a" value="123"> </body> <script> // val var val= $('input').val(); console.log(val) //css $('div').css('background-color','red') </script> </html> 设置值:变得永远是content的值
宽度(width)和高度(height)
/* 宽度 */ //获取宽度 返回匹配元素中第一个元素的宽,一个没有单位的数值 $('选择器').width() //设置宽度 $('选择器').width( value ) /* 高度 */ //获取高度 返回匹配元素中第一个元素的高,一个没有单位的数值 $('选择器').height() //设置高度 $('选择器').height( value ) 宽度(innerWidth)和高度(innerHeight)
/* 内部宽 */ // 获取 $('选择器').innerWidth(); //设置 $('选择器').innerWidth(value); /* 内部高 */ // 获取 $('选择器').innerHeight(); //设置 $('选择器').innerHeight(value); 宽度(outerWidth)和高度(outerHeight)
// 外部宽 .outerWidth() //获取第一个匹配元素 :内容+padding+border的宽 .outerWidth(true) //获取第一个匹配元素:内容+padding+border+margin的宽 .outerWidth(value) //设置多个,调整的是“内容”的宽 //外部高 .outerHeight() //第一个匹配元素:获取内容+padding+border的高 .outerHeight(true) //第一个匹配元素:获取内容+padding+border+margin的高 .outerHeight( value ) //设置多个,调整的是“内容”的高 简单示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>盒模型</title> <style> *{ margin: 0; } div{ width: 100px; height: 100px; background-color: rebeccapurple; padding: 10px; margin: 10px; border: 10px solid #ffd19b; } </style> <script src="jquery3.4.1.js"></script> </head> <body> <div></div> </body> <script> // 内容的宽 width // $('div').width(); //获取原本宽度 // $('div').width(200); //设置宽度 //内容的高 height // $('div').height(); //获取原本高度 // $('div').height(200); //设置高度 // 内容 + padding // 宽度 // $('div').innerWidth() // 高度 // $('div').innerHeight() // 内容+padding+border $('div').outerWidth() //140 $('div').outerWidth(true) //160 $('div').outerHeight() $('div').outerHeight(true) </script> </html>转载于:https://www.cnblogs.com/dengl/p/11366605.html
相关资源:jquery获取表单值