先上一波代码
<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()的操作了