简体   繁体   English

获取HTML元素的完整(原始)文本

[英]Get full (and original) text of an HTML element

Edit: It looks like we identified the solution to this problem via the comments -- which is achieved by getting value of the .outerHTML property. 编辑:看起来我们已经通过注释确定了此问题的解决方案-这是通过获取.outerHTML属性的值来实现的。 However, it still appears that at least Firefox and Chrome "normalize" original source code when outerHTML is used. 但是,当使用externalHTML时,仍然至少有Firefox和Chrome浏览器“标准化”原始源代码。 For example, outerHTML of 例如,

<div id = "a">    <!-- string is 14 characters long //-->

still returns 仍然返回

<div id="a">      <!-- string is 12 characters long //-->

Apparently, the problem would be considered solved if the formatting of the resulting string would match that of the original HTML source code. 显然,如果结果字符串的格式与原始HTML源代码的格式匹配,则可以认为该问题已解决。 Ah! 啊! Why must outerHTML adjust the original value? 为什么externalHTML必须调整原始值?

--- Having said this: --- ---说了这么多:---

I'm looking for a solution to get full text of a clicked HTML tag. 我正在寻找一种解决方案,以获取单击的HTML标记的全文。

Starting point examples (note intentional , legal but mangled formatting): 起点示例(注意故意的合法的但格式错误的格式):

<div id = "a" style ="color: blue ;">text</div>

// Returns: div
var doc = document.getElementById("id").tagName;

// Returns: array of attribute name/value pair (without = or ")
var attrs = document.getElementById("id").attributes;

How would we go about generating the following text string, when element #a is clicked: 单击元素#a时,如何生成以下文本字符串:

<div id = "a" style= "color: blue ;">

I seem to have not found a solution for this as of yet. 到目前为止,我似乎还没有找到解决方案。

What's this for? 这是用来干嘛的?

Ultimately, the goal is to determine the length in characters of the arbitrary contents of a tag. 最终,目标是确定标签任意内容的字符长度。 Assuming it can be edited in any way that produces acceptable HTML output. 假定可以用任何可接受的HTML输出方式对其进行编辑。 For example, the two cases below should return: 例如,以下两种情况应返回:

<div id=a style="color:blue">            // 28
<div id = "a" style= "color: blue ;">    // 36

Counting is the easy part. 计数是容易的部分。 It's getting the actual string of that tag, just as it appears in the source code, that is the problem. 正如它在源代码中显示的那样,它正在获取该标签的实际字符串,这就是问题所在。

Have you tried this? 你有尝试过吗?

document.getElementById('a').outerHTML

But this doesn't work in every browser i guess 但这并不适用于所有浏览器

使用externalHTML获取完整标签,然后在open标签之后去除所有内容。

var openTag = document.getElementById("a").outerHTML.split(">")[0] + ">";

This seems to do what you want: 这似乎可以满足您的要求:

http://jsfiddle.net/abalter/c3eqnLrc/ http://jsfiddle.net/abalter/c3eqnLrc/

html: HTML:

<div id="a" class="find-my-length" style="color:blue">First One</div>
<div   id="a  " class="find-my-length"   style= "color: blue ; " > Second     One </div        >

JavaScript: JavaScript的:

$('.find-my-length').on('click', function () {
    var htmlString = $(this).prop('outerHTML');
    alert(htmlString + " has " + htmlString.length + " characters.");
});

Note: The one thing that doesn't get counted is spaces between attributes. 注意:不计算的一件事是属性之间的空格。 Spaces within attributes are counted. 计算属性内的空间。

From: Get selected element's outer HTML 来自: 获取选定元素的外部HTML

What about: prop('outerHTML') ? 怎么样: prop('outerHTML')

 var outerHTML_text = $('#item-to-be-selected').prop('outerHTML'); 

And to set: 并设置:

 $('#item-to-be-selected').prop('outerHTML', outerHTML_text); 

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

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