简体   繁体   English

如何在不更改源的情况下返回结果?

[英]How to return the result without altering the source?

My HTML and JavaScript code look like this:我的 HTML 和 JavaScript 代码如下所示:

<html>
<!--...
many code 
...-->
    <button onclick="alert($('#mytable').html().wrap('<html/>') );">Show HTML Table</button>

    <table id="myTable">

        <tr's and td's>
    </table>
<!--...
many code
...-->
</html>

I want my javascript to return the table wrapped by the HTML tags but I do not want the table itself to be changed.我希望我的 javascript 返回由 HTML 标记包装的表,但我不希望表本身被更改。

You could take a copy of the table first:您可以先复制表格:

$('#mytable').clone()...

To get the actual HTML of the tag you'd need something like this plugin which I posted in another answer yesterday :要获得标签的实际 HTML,您需要类似于我昨天在另一个答案中发布的这个插件:

(function($) {
    $.fn.outerhtml = function() {
        return $('<div/>').append(this.clone()).html();
    };
})(jQuery);

So you can then do:所以你可以这样做:

alert('<html>' + $('#myTable').outerhtml() + '</html>');

See http://jsfiddle.net/alnitak/2y988/ for a working demo.有关工作演示,请参阅http://jsfiddle.net/alnitak/2y988/

This does not work.这不起作用。 .html() returns a string, not a jQuery object. .html()返回一个字符串,而不是 jQuery object。 So you cannot call wrap on it.所以你不能调用wrap

The other problem is that .html() only returns the inner HTML , it does not include the table tag.另一个问题是.html()只返回内部 HTML ,它不包含table标记。

You could .clone() the node, attach it to some dummy element and return the .html() :您可以.clone()节点,将其附加到一些虚拟元素并返回.html()

var html = ['<html><body>', 
            $('<div/>').append($('#mytable').clone()).html(), 
            '</body></html>'].join('');

Maybe this jQuery outerHTML plugin will help you.也许这个jQuery outerHTML 插件会对您有所帮助。 It will give you the code for the table, including the enclosing <table> tags.它将为您提供表格的代码,包括封闭的<table>标记。 You can maybe do something like alert("<html>" + $("#myTable").outerHtml() + "</html>") .你也许可以做类似alert("<html>" + $("#myTable").outerHtml() + "</html>")

Why not just do the following?:为什么不只做以下事情?:

alert('<html>'+$('#mytable').html()+'</html>');
$("#myTable").wrap("...");

That will wrap the table in the tag supplied to the wrap function, without altering the table itself.这会将表格包装在提供给wrap function 的标签中,而不会更改table本身。

For more information, see the jQuery API for the wrap function.有关详细信息,请参阅jQuery API以了解wrap function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改“参数变量”而没有返回变量和全局变量 - Altering “argument variables” without return and global variables 在不改变HTML源代码的情况下,在管理员TinyMCE编辑器中突出显示文本? - Highlight text in the admin TinyMCE editor without altering the HTML source? 如何从数据列表返回结果而不会出现“未定义”错误 - How to return a result from datalist without getting “undefined” error angular 如何在不提交的情况下输入和选择值返回结果 - angular how input and select value return result without submit 如何在不通过结果单独索引的情况下将匹配项作为字符串返回 - How to return a match as a string without indexing separately through the result 如何将函数的结果发送给第三个函数而不返回? - How do I send the result of a function to a third one without a return? 如何在不更改对象内部对象的情况下合并数组中的多维对象 - how to merge multidimensional objects in array without altering the objects inside objects 如何在不更改主文件的情况下撤消Core.JS代码? - How Core.JS Code be undone without altering main files? 如何在不更改尺寸的情况下使用jQuery隐藏/显示div? - How to hide/show div using jQuery without altering dimensions? 如何在DIV中垂直和水平对齐图像而不改变宽度/高度? - How to vertically and horizontally align an image in a DIV without altering width/height?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM