简体   繁体   English

jQuery加载txt文件.html()

[英]jQuery load txt file .html()

how can I decode the html contents of the .txt file when using the $('myDiv').load('mytxtFile.txt') ? 使用$('myDiv').load('mytxtFile.txt')时,如何解码.txt文件的html内容?

I know: $('<div/>').html(value).text(); 我知道: $('<div/>').html(value).text();

In other words, How do I combine these two? 换句话说,如何结合这两个?

important note: the mytextFile.txt, is the container of some of codes which jenerated with ckeditor, and i want to show the content of this txt file in my html form as a html form. 重要说明:mytextFile.txt是用ckeditor修饰的一些代码的容器,我想以html表单的html形式显示此txt文件的内容。

the codes in my txt file is: 我的txt文件中的代码是:

&lt;p&gt;hellohome&lt;/p&gt;

and I want to show it as : sss hello home sss in my html page 我想将其显示为:html页面中的sss hello home sss

tank you for your attantion 为你的失败而战

If I understand the question correctly you can do this: 如果我正确理解了问题,则可以执行以下操作:

$('myDiv').load('mytxtFile.txt', function(text) {
  $(this).text(text);
});

You can't. 你不能 load() is designed to pull fragments of HTML directly into the document. load()用于将HTML片段直接拉入文档。 If you want to preprocess the data, then load it using $.ajax instead and write your own logic for updating the element content with success . 如果要预处理数据,请改用$.ajax加载数据,并编写自己的逻辑以success更新元素内容。

success: function (data) {
    $('myDiv').text(data);  // Note: Your selector but not one that is valid for an HTML document
}

You can load the textfile with $.ajax and then add the text with " $('#YOURDIV').text(data) " 您可以使用$.ajax加载文本文件,然后使用“ $('#YOURDIV').text(data)添加文本$('#YOURDIV').text(data)

Example: Fiddler: http://jsfiddle.net/ZXMha/ 示例:提琴手: http : //jsfiddle.net/ZXMha/

HTML: HTML:

JavaScript: JavaScript的:

$.ajax({
    url: '/echo/html/',
    type: "POST",
    data: {
        html: "<p>Text echoed back to request</p>" + "<script type='text/javascript'>$('target').highlight();<\/script>",
        delay: 0
    },
    success: function(data){
        $('#text').text(data);
    }
})

Output: 输出:

<p>Text echoed back to request</p><script type='text/javascript'>$('target').highlight();</script>

Note: Post is used in this example to simulate an ajax call where html is returned. 注意:在本示例中,使用Post来模拟返回html的ajax调用。 See http://doc.jsfiddle.net/use/echo.html for Fiddler echo HTML Options. 有关Fiddler回声HTML选项的信息,请参见http://doc.jsfiddle.net/use/echo.html

For your second question: you can simply post the text with .html(data) into your div. 关于第二个问题:您只需将带有.html(data)的文本发布到div中。

Code: 码:

$.ajax({
    url: '/echo/html/',
    type: "POST",
    data: {
        html: "&lt;p&gt;hellohome&lt;/p&gt;",
        delay: 0
    },
    success: function(data){
        $('#text').html(data);
    }
})

Fiddler: http://jsfiddle.net/KWpUC/ 提琴手: http : //jsfiddle.net/KWpUC/

I think you're after something like this: 我认为您正在追求这样的事情:

 $('<div/>').load('mytextFile.txt', function(textStr) {
    var htmlStr = $(this).html(textStr).text(); 
    $(this).html(htmlStr);
 }).appendTo('form');

That'll effectively encode the html entities, return a properly formed htmlStr , which you can use to set the html() of the element. 这将有效地编码html实体,返回格式正确的htmlStr ,您可以使用它来设置元素的html()

Here's a demo fiddle 这是一个演示小提琴

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

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