search

Home  >  Q&A  >  body text

javascript - $(this).innerHTML takes out undefined, why

<!DOCTYPE html>
<html>
<head>
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">

function preview(){
            temp = $(this).innerHTML;
            testwin= open("", "testwin","status=no,menubar=yes,toolbar=no");
            testwin.document.open();
            testwin.document.write(temp);
            testwin.document.close();
        }

</script>
<table width="100%" class="am-table am-table-bordered am-table-radius am-table-striped">
                 
                    <tbody>
                    
                    <tr>
                       
                     <td  onclick="preview()">dghfh</td>
                    </tr>
                    </tbody>
                </table>

</html>
習慣沉默習慣沉默2861 days ago748

reply all(8)I'll reply

  • phpcn_u1582

    phpcn_u15822017-05-19 10:46:11

    Pass a parameter to the function preview() you declared, then give the parameter value to the variable temp, and finally change the call to onclick=" preview(this.innerHTML)".
    Personal test, it works
    As for $(this).innerHTML This in is the browser object, not the jq object

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-19 10:46:11

    The function preview written in onclick on the element points to the global object window, not to the element

    reply
    0
  • 迷茫

    迷茫2017-05-19 10:46:11

    $(this) is a jq object, so use $(this).html()

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-19 10:46:11

    $(this)[0].innerHTML; like this

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-19 10:46:11

    You can stop and take a look at what $(this) you wrote is, it should refer to window, not the td you want

    reply
    0
  • 阿神

    阿神2017-05-19 10:46:11

    $(this) is the context environment wrapped by jquery, and its specified method is required to obtain the internal static fragment.
    Two ways to obtain:

    $(this).html();
    $(this).prop('innerHTML');

    reply
    0
  • 为情所困

    为情所困2017-05-19 10:46:11

    Native writing: this.innerHTML
    jquery: $(this).html()
    jq+Native: $(this)[0].innerHTML or this.get(0).html()

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-19 10:46:11

    Do you want native or jq

    reply
    0
  • Cancelreply