jQuery里面的选择属性和修改属性

在jQuery中,我们可能会给元素添加自有属性,也有可能添加自定义属性,所以在修改时,jQuery给我们提供了相对应的选择方法

1.prop()方法

prop()方法是处理html元素自带固有属性时使用

<a href="www.123.com" target="_self" class="123"></a>
<script>
	$(function() {
		$('.123').prop('href');
		console.log($('.123').prop('href'));
	})
</script>

href,taget,class都是a标签的自由属性,用prop()方法可以得到

<a href="www.123.com" target="_self" class="123"></a>
<script>
	$(function() {
		$('.123').prop('href');
		console.log($('.123').prop('href'));
		$('.123').prop('href','www.456.com');
		console.log($('.123').prop('href'));
	})
</script>

当我们需要对这些元素固有属性修改时只需 prop(‘属性’,‘值’),就可以实现修改

2.attr()方法

attr()方法是处理html元素自定义属性时使用

<a href="www.123.com" target="_self" class="123" date-index = 0></a>
<script>
	$(function() {
		$('.123').attr('date-index');
		console.log($('.123').attr('date-index'));
	})
</script>

修改属性值得方法与prop()一样

3.html(),text(),val()

<div>
	<a href="#">我是文本</a>
	<input type="text" value="我是文本">
	</div>
<script>
	$(function() {
		$('div').html();
		console.log($('div').html());
		$('div').text();
		console.log($('div').text());
		$('input').val();
		console.log($('input').val());
	})
</script>

html()方法会回去匹配元素内的所有内容,包括子节点
text()方法只会得到匹配元素内的文本内容
在input表单元素中,通过val()方法获取内容
在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值