dom节点操作元素

先上一波代码

<html>
<head>
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
</head>
<body id="body">
<sspan id="test1" name="test1" value="test1的value" goodId="6">test1的文本内容</sspan>
<a href="javascript:void(0)" onclick="test(this)" goodId="6" price="666" number="6000">测试</a>
</body>
<script type="text/javascript">
    console.log('$("#test1").text() ====='+$("#test1").text())
    console.log('$("#test1").val() ====='+$("#test1").val())
    $("[goodId]").each(function () {
        console.log('$("[goodId]") $(this).attr("value") ====='+$(this).attr("value"));
        console.log('$("[goodId]") $(this).attr("goodId") ====='+$(this).attr("goodId"));
    })
    function test(ele) {
        console.log($(ele).attr("goodId"))
        console.log($(ele).attr("price"))
        console.log($(ele).attr("number"))
    }
</script>
</html>

这个代码难度没有多大
但是有个疑问console.log('$("#test1").val() ====='+$("#test1").val()) 这个没有取到值
别的运行效果如下:

$("#test1").text() =====test1的文本内容
test.html:11 $("#test1").val() =====
test.html:13 $("[goodId]") $(this).attr("value") =====test1的value
test.html:14 $("[goodId]") $(this).attr("goodId") =====6
test.html:13 $("[goodId]") $(this).attr("value") =====undefined
test.html:14 $("[goodId]") $(this).attr("goodId") =====6
按钮点击之后的控制台===》》》//这是为了和上面表示区分
VM69:1 Uncaught SyntaxError: Invalid or unexpected token//这是为了和上面表示区分
6
666
6000

这样就能取到之前藏到a标签的值了 这样省的去parent()的操作了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值