简体   繁体   English

从一组jQuery对象生成HTML?

[英]generate HTML from a set of jQuery objects?

I am trying to modify an HTML string (things like adding class to one of its children). 我正在尝试修改HTML字符串(将类添加到其子级之一)。 In my code I have to used a container as a midway to ouput $html as a string. 在我的代码中,我必须使用container作为中途输出$html作为字符串。 Does jQuery provide any function to do this? jQuery是否提供任何功能来做到这一点?

  html = "<p>title</p><div><ul class='www'></ul>something</div>";
  $html = $(html);
  $html.filter('div').find('ul').addClass('xxx');
  container = $('<div></div>');
  html = container.html($html)[0].innerHTML; //output "<p>title</p><div><ul class='www xxx'></ul>something</div>"

Nope. 不。

There is no escape to interacting with the DOM (this is creating or selecting an existing element), the best you can try is to document.write your string but you'll need to scape the HTML so it doesn't gets interpreted as HTML but text. 与DOM交互是无可避免的(这是创建或选择现有元素),最好的尝试是对document.write字符串进行编码,但是您需要对HTML进行转义,以免将其解释为HTML但是文字。 Notice that document.write only works before the document finishes loading. 请注意, document.write仅在文档完成加载之前有效。

I don't know your needs but I can't think of a good use case for this, your jQuery should be better for most cases. 我不知道您的需求,但是我想不出一个很好的用例,您的jQuery在大多数情况下应该更好。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM