javascript - 在 JavaScript 中将变量等同于 "this"

标签 javascript jquery html

我正在尝试将表行存储在窗口变量内

window.row = this

哪里this = <tr><td>Content 1</td><td>Content 2</td></tr>

但是,作为一个类添加到 this

<tr class="checked"><td>Content 1</td><td>Content 2</td></tr>

window.row也更改为上面。

如何防止window.row每次 this 都会被更改被改变了。

最佳答案

如果你想要row为字符串 "<tr><td>Content 1</td><td>Content 2</td></tr>" ,使用 outerHTML :

window.row = this.outerHTML;

如果你想要row到像 this 这样的 HTML 元素,使用 cloneNode :

window.row = this.cloneNode(true); // Use true argument to clone descendants too

请注意,克隆方法不会复制事件监听器或属性。

关于javascript - 在 JavaScript 中将变量等同于 "this",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23091288/

相关文章:

javascript - 使用拦截器处理 Ionic App 中的离线数据

javascript - 在 FTP 上实时缩小 CSS 和 JS

html - CSS Grid auto-fit + minmax 添加幻像行

javascript - Google Maps Info Box - 它可以存在于 map 容器之外吗?

javascript - Meteor 模板助手不返回值

javascript - 打开前选择2 scrollIntoView

javascript - 使选择的下拉列表只读(未禁用)

javascript - 如何在上传图片时发送一些额外的字段?

Jquery从表单中的选择/选项字段中显示div中的多个图像

html - CSS3 - 使用 :focus issue 显示工具提示