我正在尝试编写一系列函数来根据数组的值修改表单。 我的具体问题是,对于我的函数,我正在接受一个 html 字符串。我将该 html 应用于 div 元素并使用 jquery 对 html 进行修改。
具体的修改类型主要是$("#target").show()以及到处写一些文字。
我遇到的问题是,我的研究或在线调查这个问题都没有让我找到一种可以将修改后的 html 转换为字符串的方法。条件是我希望这个字符串保留我从 $("#target").show() 类型的修改中所做的更改。
是否可以对 jquery 对象进行这些类型的修改,然后生成一个以纯字符串反射(reflect)这些更改的字符串?
更新感谢我尝试过的快速回复 .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 并且更改没有保留,所以我试图找出是否有办法做到这一点。
最佳答案
jQuery 方法 .html()
返回 DOM 元素内容的 HTML 字符串。
var html_string = $("#div").html();
关于javascript - 通过 jquery 修改 HTML 字符串并生成另一个反射(reflect)所做更改的 html 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30467784/