ddddd

$("this")和$(this)和this

作者:程序员11 时间:2021-12-24 人气:417 QQ交流群\邮箱:1003265987@qq.com
通过$("this")获得this标签对象,使用text()方法获得span的内容;使用$(this)获得当前对象,相当于使用$("#test")获得对象,通过text()方法获得span的内容,只能在bind的方法中使用
要展示的内容
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript" src="jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
<p><span id="test">这是测试内容</span></p>
<button id="p1" onclick="getthis()">sss<this>获取this元素内容</this></button>
<button id="p2">sss2</button>
<button onclick="btnClick(this)" data-type="123">sss3</button>
<button id="p3" data-type="1234">sss4</button>
<script>
    $("#test").bind("click",function () {
        console.log($("#test"))
        console.log($(this))
        var data = $("#test").text();
        alert(data)//有值
 var data = $(this).text();
        alert(data)//有值
 })
    $("#p2").bind("click",function () {
        console.log($("#p2"))
        console.log($(this))
        var data = $("#p2").text();
        alert(data)//有值
 var data = $(this).text();
        alert(data)//有值
 })
    $("#p3").click(function (e) {
        console.log(e)
        var data = e.currentTarget.dataset.type;
        alert(data)//有值
 var data = $(e).data('type');
        alert(data)//没有值
 var data = $(this).text();////这是一个jQuery对象可以调用jQuery方法
 alert(data)//有值
 })
    function getthis() {
        var data = $("this").text();
        alert(data)//有值
 var data = $(this).text();
        alert(data)//没有值
 var data = $("#p1").text();
        alert(data)//有值
 }
    function btnClick(e) {
        console.log(e)//这是一个jQuery对象可以调用jQuery方法<button onclick="btnClick(this)" data-type="123">sss3</button>
 var data = $(e).text();
        alert(data)//有值
 var data = $(e).data('type');
        alert(data)//有值
 }
</script>
</body>
</html>


温馨提示:

欢迎阅读本文章,觉得有用就多来支持一下,没有能帮到您,还有很多文章,希望有一天能帮到您。

$("this")和$(this)和this---相关文章


评论区

ddddd

程序员-学习的网站-想学习编程的码农可以进来看看

首页

视频教程

购物车

我的订单