javascript - $(this).innerHTML entfernt undefiniertes Warum
習慣沉默
習慣沉默 2017-05-19 10:44:11
0
8
750
<!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>
習慣沉默
習慣沉默

Antworte allen(8)
phpcn_u1582

给你声明的函数 preview() 传一个参数,然后把参数的值给变量temp,,最后调用改为 onclick=" preview(this.innerHTML)".
亲测可行
至于 $(this).innerHTML 中的this 是浏览器对象,不是jq对象

漂亮男人

写在元素上onclick里面的函数preview,里面的this指向全局对象window,而不是指向该元素

迷茫

$(this)是jq对象,要用也是$(this).html()啊

大家讲道理

$(this)[0].innerHTML;这样

曾经蜡笔没有小新

你可以打断点看一下你写的这个$(this)是啥,这里应该指的是window,而不是你想要的td

阿神

$(this)是jquery包装的上下文环境,需要其指定方法获取内部静态片段。
两种方式获取:

$(this).html();
$(this).prop('innerHTML');
为情所困

原生写法: this.innerHTML
jquery: $(this).html()
jq+原生: $(this)[0].innerHTML 或者是 this.get(0).html()

習慣沉默

到底想原生还是jq

Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage