温馨提示×

jquery如何获取标签名称

养鱼的猫咪
225
2021-05-19 14:04:45
栏目: 编程语言

使用jquery获取标签名称的方法:1.新建html项目,引入jquery;2.创建p标签,设置id属性;3.添加button按钮,绑定onclick点击事件;4.通过id获取标签对象,使用prop(tagName)方法获取标签名称;


jquery如何获取标签名称


具体步骤如下:

1.首先,新建一个html项目,并在项目中引入jquery;



<script type="text/javascript" src="/static/jquery-2.1.4.min.js"></script>

2.引入jquery后,在项目中创建一个p标签,并设置id属性,用于测试;




<p id="text"></p>

3.p标签创建好后,添加一个button按钮,并绑定onclick点击事件;



<button omClick="set()"></button>

4.最后,按钮添加好后,在点击事件中通过id获取标签对象,在使用prop('tagName')方法即可获取标签名称;


function set(){

var res = $('#text').prop('tagName');

alert(res);

}



0