attr()方法:获取/设置元素属性" class="reference-link">1.attr()方法:获取/设置元素属性

需先引入JQuery

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
html

  1. <form action="index.php" method="POST">
  2. <input type="text" name="username" />
  3. <input type="password" name="password" />
  4. </form>

attr(name):getter 获取元素属性" class="reference-link">1.1 attr(name):getter 获取元素属性

例如获取表单属性

  1. const form=$("form");
  2. console.log(form.attr("action"));//index.php

attr(name,value):setter 设置元素属性" class="reference-link">1.2 attr(name,value):setter 设置元素属性

例如设置表单属性

  1. const form=$("form");
  2. form.attr("action","admin/index.php");
  3. console.log(form.attr("action"));//admin/index.php

第二个参数可以使用回调函数

  1. form.attr("action",()=>{
  2. let method=form.attr("method").toUpperCase();
  3. return method==="GET"?"index.php?username=Jy":"admin/index.php";
  4. });
  5. console.log(form.attr("action"));//如果为get则返回index.php?username=Jy;post则返回admin/index.php

css():获取/设置元素的行内样式" class="reference-link">2.css():获取/设置元素的行内样式

需先引入JQuery

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
css

  1. <style>
  2. form{
  3. width: 100px;
  4. }
  5. input{
  6. width: 95%;
  7. }
  8. </style>

html

  1. <form action="index.php" method="POST">
  2. 帐号: <input type="text" name="username" />
  3. 密码:<input type="password" name="password" />
  4. </form>

css(name):getter 获取元素行内样式" class="reference-link">2.1 css(name):getter 获取元素行内样式

例如获取表单宽度

  1. const form=$("form");
  2. console.log(form.css("width"));//100px

使用原生style只能获取style上的行内样式

  1. console.log(document.querySelector("form").style.width);//无法获取

css样式表里的样式必须使用计算样式才可以获取

  1. console.log(window.getComputedStyle(document.querySelector("form"),null).getPropertyValue("width"));//100px

css(name,value):setter 设置元素行内样式" class="reference-link">2.2 css(name,value):setter 设置元素行内样式

例如设置表单边框;多个参数用{}包裹,并用,隔开即可

  1. const form=$("form");
  2. form.css("border","2px solid red");//边框变为红色;

第二个参数可以使用回调函数

  1. form.css("color",()=>{
  2. const colors=["red","blue","yellow"];
  3. let i=Math.floor(Math.random() * colors.length);//Math.floor:向下取整;Math.random:0-1之间随机取值
  4. return colors[i];
  5. });
  6. //字体颜色将随机更换

val():设置/获取表单控件的value属性" class="reference-link">3. val():设置/获取表单控件的value属性

需先引入JQuery

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
html

  1. <form action="index.php" method="get">
  2. 帐号:<input type="text" name="username" value="Jy" />
  3. 密码:<input type="password" name="password" value="123456" />
  4. 性别:
  5. <label for="man"></label>
  6. <input type="radio" name="sex" id="man" value="1" />
  7. <label for="woman"></label>
  8. <input type="radio" name="sex" id="woman" value="0" checked/>
  9. </form>
  1. <script>
  2. console.log($("input[name='username']").val());//Jy
  3. console.log($("input:password").val());//123456
  4. console.log($("input:radio:checked").val());//0
  5. $("input[name='username']").val("Jy2");//账号的值变为Jy2
  6. </script>

html()等同与innerHTML、text()等同于innerText/textContent" class="reference-link">4. html()等同与innerHTML、text()等同于innerText/textContent

更多相关文章

  1. MVC控制器类的访问、参数解析、api接口数据获取并渲染
  2. Python再学习之列表介绍
  3. Spark2.x精通:ShuffleReader过程源码深度剖析
  4. linux系统中普通用户获取root权限
  5. python获取文件md5
  6. mvc控制器的访问与参数解析和API接口获取数据
  7. Jquery基础知识
  8. jqueryDom操作与ajax请求
  9. JavaScript初学习/之classList:动态设置元素类、dataset: 读写自

随机推荐

  1. 调整ChartJS中的X轴步骤
  2. React组件的属性PropTypes
  3. 用户打开一个DropDown关闭其他DropDowns
  4. 无法从按钮onclick事件ASP.NET 4调用Java
  5. 从动态生成的音频标签中收听audio.ended
  6. 获取当前时间getDate()注意点
  7. 使用Angularjs根据值选择复选框
  8. 通过IE中的DOM访问帧
  9. 一张图轻松搞懂javascript event对象的cl
  10. 使用jackson json将属性添加到json字符串