jQuery get or set the attribute value of the element

jQuery get or set the attribute value of the element

  1. Get element attribute value
  • Inherent properties ($('div).prop('color'))
  • Custom attributes ($('div).attr('index'))
    //html代码
    <a href="www.baidu.com" title="百度">百度</a>
    <input type="checkbox" checked='checked'>
    <div index="1"></div>
	//jQuery代码(记得引入jQuery文件)
	console.log($('a').prop('href'));//www.baidu.com
	console.log($('a').prop(''title));//百度
  • Custom attributes
		//jQuery代码(记得引入jQuery文件)
	console.log($('div').attr('index'));//1
  1. Set the attribute value of the element
//jQuery代码(记得引入jQuery文件)
$('a').prop('href','http://www.jidong.com');//将a标签的href属性改为www.jidong.com
$('input').prop('type',"submit');//将单选按钮改为提交框
//jQuery代码(记得引入jQuery文件)
$('div'.attr('index',2));//将自定义属性index的值改为2

Guess you like

Origin blog.csdn.net/Angela_Connie/article/details/110707443