在背景操纵'临时dom'

时间:2011-12-31 14:50:58

标签: javascript jquery

这是一个有点棘手的问题。我正在使用将结果插入DOM的第三方库。

示例:

$('#puthere').thirdpartyplugin();

这将调用thirdpartyplugin并操纵HTML元素#puthere的结果。

我的问题是,如何将结果输出到JavaScript变量而不是DOM元素?

var plainOutput =  $.thirdpartyplugin();  alert(plainOutput);

我不想操纵用户可见的HTML元素。我只想打电话给alert(plainOutput)结果。

1 个答案:

答案 0 :(得分:4)

创建一个临时元素:

var $out = $('<div />');
$out.thirdpartyplugin();
alert($out.html()): // or .text();

这可能会也可能不会起作用,具体取决于插件的功能。

如果插件按规则播放并支持方法链接,您还可以执行以下操作:

var $out = $('<div />').thirdpartyplugin().html();