javascript - $(this).innerHTML takes out undefined, why
習慣沉默
習慣沉默 2017-05-19 10:44:11
0
8
747
<!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>
習慣沉默
習慣沉默

reply all(8)
phpcn_u1582

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

漂亮男人

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

迷茫

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

大家讲道理

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

曾经蜡笔没有小新

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

阿神

$(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');
为情所困

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

習慣沉默

Do you want native or jq

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template