简体   繁体   English

如何使用jQuery获取td中包含的HTML?

[英]How do I get the HTML contained within a td using jQuery?

I have a table cell that contains HTML, eg, 我有一个包含HTML的表格单元格,例如,

    <td id="test">something&trade; is here</td>

I have an input field that I want to use to edit the HTML inside the table cell, eg, 我有一个输入字段,我想用它来编辑表格单元格内的HTML,例如,

    <input type="text" id="editor" value="">

I need to get the string something&trade; is here 我需要得到字符串something&trade; is here something&trade; is here from the table cell so I can put it into the <input> for editing. something&trade; is here从表单元格,所以我可以把它放入<input>用于编辑。 I have tried 我试过了

    var txt=$("#test").text();
    var htm=$("#test").html();

Both of them are returning " something™ is here " rather than the raw HTML - I have a breakpoint in Firebug immediately after setting the two test values, and that's what I'm seeing. 他们两个都返回“ something are here ”,而不是原始HTML-设置两个测试值后,我在Firebug中立即有了一个断点,这就是我所看到的。

Reading the jQuery documentation , I really expected the .html() method to return the raw HTML I'm looking for, but that's not what is happening. 在阅读jQuery文档时 ,我确实希望.html()方法返回我正在寻找的原始HTML,但是事实并非如此。

I know that Javascript doesn't have an encoder like PHP's htmlspecialchars() function and that I have to work around that, but all four of these operations produce the same results: 我知道Javascript没有像PHP的htmlspecialchars()函数那样的编码器,我必须解决这个问题,但是所有这四个操作都产生相同的结果:

    var enchtm=$("<div/>").text(htm).html();
    var enctxt=$("<div/>").text(txt).html();
    var htmenc=$("<div/>").html(htm).text();
    var txtenc=$("<div/>").html(txt).text();

Every permutatation puts " something™ is here " in the editfield, not the raw HTML. 每个排列都会在编辑字段中放置“ something™在此处 ”,而不是原始HTML。

How do I get the string something&trade; is here 我如何得到字符串something&trade; is here something&trade; is here from the table cell into the <input> so I can edit it? something&trade; is here从表格单元格进入<input>所以我可以对其进行编辑?

It doesn't exist. 它不存在。 Entities are decoded before the DOM is produced, and .html() (which is really just a wrapper for the innerHTML property) doesn't re-encode it because there's no reason for it to -- something™ is exactly as valid a representation of the HTML as something&trade; 实体在产生DOM之前就已解码, .html() (实际上只是innerHTML属性的包装器)不会对其进行重新编码,因为没有理由这样做something™确实是有效的表示形式HTML作为something&trade; is. 是。 There is no "completely raw" (pre-character-decoding) view of the HTML provided by the browser. 浏览器没有提供HTML的“完全原始”(字符解码前)视图。

Suggestion: provide the initial value as the value attribute of the input, instead of having it as the content of the div, so that the flow of data is always one way and this problem doesn't occur. 建议:提供初始值作为输入的value属性,而不是将其作为div的内容,这样数据流始终是一种方式,并且不会发生此问题。

As the other answers have indicated, what I was trying to do is literally impossible - the original HTML source code that was used to populate the table cell no longer exists in the browser by the time it gets written to the DOM document. 正如其他答案所表明的那样,我试图做的事情实际上是不可能的-用于填充表单元格的原始HTML源代码在写入DOM文档时已不再存在于浏览器中。

The way I worked around this was using a title attribute on the table cell, eg, 解决此问题的方法是在表格单元格上使用title属性,例如,

    // in the PHP/HTML source document
    <?php $text='something&trade; is here'; // the contents of the table cell ?>
    <td id="test" title="<?php echo htmlspecialchars($text) ?>"><?php echo $text ?></td>

Now the Javascript is relatively simple: 现在,Javascript相对简单:

    $("#editor").val($("#test").attr("title"));

The problem with this is that some of these table cells are supposed to have tooltips - which use the title attribute - and some are not. 这样做的问题是,这些表单元格中的某些应该具有工具提示-使用title属性-某些则没有。 Fortunately for me, the table cells that may contain HTML that needs to be edited never need to show a tooltip, and the ones that show tooltips have simple text that can be retrieved using the .text() method. 对我来说幸运的是,可能包含需要编辑的HTML的表格单元永远不需要显示工具提示,而显示工具提示的表格单元具有可以使用.text()方法检索的简单文本。 I can set a class attribute on the cells that need a tooltip, eg, 我可以在需要工具提示的单元格上设置类属性,例如,

    <td class="tooltipped" title="This is a tooltip">Hover here!</td>

I can then use jQuery's .not() method to suppress the tooltips on the cells were the title is being used for storing encoded HTML: 然后,我可以使用jQuery的.not()方法来抑制单元格上的工具提示(如果标题用于存储编码的HTML):

    // suppress browser's default tooltip on td titles
    $("td[title]").not(".tooltipped").mouseover(function()
    {   var elem=$(this);
        elem.data("title",elem.attr("title"));
        // Using null here wouldn't work in IE, but empty string does
        elem.attr("title","");
    }).mouseout(function()
    {   var elem=$(this);
        elem.attr("title",elem.data("title"));
    });

Javascript has escape function but it won't help you in this case, I just did a trick for you, but stay to get best answer. Javascript具有escape功能,但在这种情况下它不会为您提供帮助,我只是为您提供了一个窍门,但请留下以获取最佳答案。

 var htm = $("#test").html().replace(/™/g, '&trade;'); $('#editor').val(htm); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td id="test">something&trade; is here</td> </tr> </table> <input type="text" id="editor" value=""> 

I don't think that you would be able to get the exact value as html parses the document and displays the it on the browser and I don't know of any keyword or library that helps to find the exact contents of the element. 我认为在html解析文档并将其显示在浏览器中时,您将无法获得确切的值,而且我不知道任何有助于查找元素确切内容的关键字或库。 But you would use Ajax in this case and get all the contents of the file in the string format to get the exact value. 但是在这种情况下,您将使用Ajax并以字符串格式获取文件的所有内容以获取确切的值。

 var client = new XMLHttpRequest(); client.open('GET', './file.html'); // get the file by location client.onreadystatechange = function() { if (client.readyState === 4) { if (client.status === 200) { // when the file is ready var file_contents = (client.responseText); // the whole file is stored in the string format in the variable get_value(file_contents); // function get_value fetches the exact value } } } client.send(); function(str_file) { var indextest = str_file.indexOf("test"); // fetches the index of test var indexclosing = str_file.indexOf("</td>"); // fetches the index of closing td tag var td_content = ""; var condition = false; for (var i = indextest; i < indexclosing; i++) { if (str_file.charAt(i - 1) == ">") { condition = true; // the condition is true when the previous character is '>' } if (condition) { // when the condition is true start storing the value in the td_content element td_content += str_file.charAt(i); } }; console.log(td_content) // display it }; 

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

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