【问题标题】:Modifying an HTML string via jquery and producing another html string that reflects the changes made?通过 jquery 修改 HTML 字符串并生成另一个反映所做更改的 html 字符串?
【发布时间】:2015-08-08 16:08:26
【问题描述】:

我正在尝试编写一系列函数来根据数组的值修改表单。 我的具体问题是,对于我的函数,我正在接受一个 html 字符串。我将该 html 应用于 div 元素并使用 jquery 对 html 进行修改。

具体的修改类型主要是$("#target").show()和这里那里写一些文字。

我遇到的问题是,我的研究或在线调查这个问题都没有让我找到一种方法,然后我可以将修改后的 html 转换为字符串。条件是我希望这个字符串保留我对 $("#target").show() 类型的修改所做的更改。

是否可以对 jquery 对象进行这些类型的修改,然后生成一个以纯字符串形式反映这些更改的字符串?

更新感谢您的快速回复,我试过了 .html() .innerhtml .outerhtml 和其他一些方法

我想要找出的主要问题是: 我拿一个html字符串。 使用 .html() 将其应用于 div 并对其进行修改,以便不显示特定元素。 然后我想将它转换回一个 html 字符串,这样当我再次应用它时,我使用 .show() 和 .hide() 方法使用 jquery 操作的那些相同元素将继续被隐藏和显示。

就像我说的,我尝试使用 .html 和 .innerhtml 将修改后的元素转换为 html,当我再次应用它时,jquery/动态修改消失了

希望这能让一切更容易理解:

 function main_selector(notiftype,array){
   /**
   - a function that recieves a type parameter and an array that invokes
   the functions listed below
   **/
 var strings;
 strings=importhtml();
 strings=manipulateHtml(strings,notiftype,array);
 strings= elemanipulateHtml(strings,notiftype,array);
 strings=btnmanipulateHtml(strings,notiftype,array);
 writeHTML(strings);
 }

   function importHtml(   ){
   /**
   - a function that will import the html file and return it as a
     a html string for manipulation
   **/

   var htmlstrings;
          $.ajax({
                   url: "/*******",
                   global: false,
                   type: 'POST',
                   async: false,
                   success: function(result) {
                htmlstrings=result;}
          });
          return htmlstrings;
   }

 function elemanipulateHtml(htmlstring,notiftype,array){
   /**
   - a function that will accept a string of html, a type parameter
     and an array and will determine based on the type and the contents
     of the array which elements in the form are shown, and what they
     contain.
 }

^ 我的问题是我需要一种方法来确保在执行完 $("#target").show(); elemanipulate 中的类型语句。我可以将修改后的 html 转换为一个字符串,该字符串将保留 #target 被隐藏的事实。我已经尝试使用 .html 并且没有保留更改,所以我试图找出是否有办法做到这一点。

【问题讨论】:

  • 当然,有办法做到这一点。你都尝试了些什么?发布您的代码,最好是在小提琴中。
  • 在修改后的 DOM 元素上调用 .html(),这将返回 HTML 字符串。

标签: javascript jquery html web-development-server


【解决方案1】:

jQuery 方法 .html() 返回 DOM 元素内容的 HTML 字符串。

var html_string = $("#div").html();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2018-02-05
    • 1970-01-01
    • 2020-07-19
    相关资源
    最近更新 更多