简体   繁体   English

是否可以为html生成结束注释

[英]Is it possible to generate end comments for html

Is it possible to generate comments for closing div tags, lets take this ex. 是否可以生成关闭div标签的注释,让我们来看看这个。 into consideration normal HTML : 考虑普通的HTML

<div id="content">
...
...buch of html or whateve
</div>

with comments : 评论:

<div id="content">
    ...
    ...buch of html or whateve
    </div><!--End of content-->

and so on go trough each div element and comment the end of it ? 等等每个div元素并评论它的结尾?

using jQuery, this is very simple. 使用jQuery,这很简单。

jQuery('div').after('<!--end of content-->');

EDIT: 编辑:

jQuery('div').each(function(){ jQuery(this).after('<!-- end of '+jQuery(this).id + '-->');});

Here is a possible solution, in PHP, using DOM : 这是PHP中使用DOM的可能解决方案:
Using PHP allows you to save it, or whatever you need to ; 使用PHP可以保存它,或者你需要的任何东西; and as it's using DOM, which is quite standardized, translating this to another language shouldn't require too much work. 因为它使用的DOM非常标准化,所以将它翻译成另一种语言不需要太多的工作。
(And, judging from a comment on your question, you didn't exclude other languages that JS) (并且从对您的问题的评论判断,您没有排除JS的其他语言)

$html = <<<HTML
<div id="content">
...
...buch of html or whateve
</div>
HTML;

$dom = new DOMDocument();
$dom->loadHTML($html);
$divs = $dom->getElementsByTagName('div');
for ($i = $divs->length - 1 ; $i > -1 ; $i--) {
    $div = $divs->item($i);
    if ($div->hasAttribute('id')) {
        $id = $div->getAttribute('id');
        $comment = $dom->createComment("End of {$id}");
        if($div->nextSibling) {
            $div->parentNode->insertBefore($comment, $div->nextSibling);
        } else {
            $div->parentNode->appendChild($comment);
        }   
    }
}
echo $dom->saveHTML();


Which gets you the following HTML source : 它为您提供以下HTML源代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<div id="content">
...
...buch of html or whateve
</div>
<!--End of content-->
</body></html>


A couple of things to note : 有几点需要注意:

  • DOM allows one to load and parse non-valid HTML DOM允许用户加载和解析无效的HTML
  • And generates valid-HTML 并生成有效的HTML


And, about what this does : 而且,关于它的作用:

  • Load the HTMl string, using DOMDocument 使用DOMDocument加载HTMl字符串
  • Search for all <div> tags 搜索所有<div>标签
  • Foreach <div> tag : Foreach <div>标签:
    • If it has an id attributes, 如果它有id属性,
    • Get its value 获得它的价值
    • Create a comment based on that value 根据该值创建评论
    • And add it to the DOM, after the </div> tag 并在</div>标记之后将其添加到DOM

Another solution, thinking about it, would probably have been to use XPath, instead of getElementsByTagName + hasAttribute ... 考虑它的另一个解决方案可能是使用XPath,而不是getElementsByTagName + hasAttribute ...

var divs = document.getElementsByTagName("div");
for (var d = divs.length-1; d >= 0; --d) {
   var div = divs[d];
   var id = div.id;  // d.getAttribute("id")
   if (id) {
     var cmt = document.createComment("End of " + id);
     div.parentNode.insertBefore(cmt, div.nextSibling);
   }
}

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

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