gpt4 book ai didi

javascript - 找出 dom 中的元素在 Javascript 中出现在哪个行号上?

转载 作者:行者123 更新时间:2023-12-03 13:36:38 26 4
gpt4 key购买 nike

虽然我从来没有听说过这个,但是是否可以使用 JS 从 DOM 中检索一个节点,然后找出该节点出现在文件的哪一行?

我对任何东西都持开放态度,替代浏览器插件/附加组件等......它不需要是跨浏览器的。

考虑到某些 JS 调试器能够在脚本标记中找到行号,我认为这可能以某种方式实现,但我并不完全确定。

最佳答案

好吧,原谅我这有多大。我认为这是一个非常有趣的问题,但在玩弄它时,我很快意识到 innerHTML 及其同类在维护空格、注释等方面是非常不可靠的。考虑到这一点,我回过头来实际拉下一份完整的来源,这样我就可以绝对确定我得到了完整的来源。然后我使用 jquery 和一些(相对较小的)正则表达式来查找每个节点的位置。尽管我确定我错过了一些边缘情况,但它似乎运作良好。而且,是的,是的,正则表达式和两个问题,等等等等。

编辑:作为构建 jquery 插件的练习,我修改了我的代码,使其作为独立的 plugin 运行得相当好。与 example类似于下面找到的 html(我将留在这里以供后代使用)。我试图让代码稍微健壮一些(例如现在处理引用字符串中的标签,例如 onclick),但最大的剩余错误是它无法解释对页面的任何修改,例如附加元素。我可能需要使用 iframe 而不是 ajax 调用来处理这种情况。

<html>
<head id="node0">
<!-- first comment -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<style id="node1">
/* div { border: 1px solid black; } */
pre { border: 1px solid black; }
</style>
<!-- second comment -->
<script>
$(function() {

// fetch and display source
var source;
$.ajax({
url: location.href,
type: 'get',
dataType: 'text',
success: function(data) {
source = data;


var lines = data.split(/\r?\n/);
var html = $.map(lines, function(line, i) {
return ['<span id="line_number_', i, '"><strong>', i, ':</strong> ', line.replace(/</g, '&lt;').replace(/>/g, '&gt;'), '</span>'].join('');
}).join('\n');

// now sanitize the raw html so you don't get false hits in code or comments
var inside = false;
var tag = '';
var closing = {
xmp: '<\\/\\s*xmp\\s*>',
script: '<\\/\\s*script\\s*>',
'!--': '-->'
};
var clean_source = $.map(lines, function(line) {
if (inside && line.match(closing[tag])) {
var re = new RegExp('.*(' + closing[tag] + ')', 'i');
line = line.replace(re, "$1");
inside = false;
} else if (inside) {
line = '';
}

if (line.match(/<(script|!--)/)) {
tag = RegExp.$1;
line = line.replace(/<(script|xmp|!--)[^>]*.*(<(\/(script|xmp)|--)?>)/i, "<$1>$2");
var re = new RegExp(closing[tag], 'i');
inside = ! (re).test(line);
}
return line;
});

// nodes we're looking for
var nodes = $.map([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], function(num) { return $('#node' + num) });

// now find each desired node in both the DOM and the source
var line_numbers = $.map(nodes, function(node) {
var tag = node.attr('tagName');
var tags = $(tag);
var index = tags.index(node) + 1;

var count = 0;
for (var i = 0; i < clean_source.length; i++) {
var re = new RegExp('<' + tag, 'gi');
var matches = clean_source[i].match(re);
if (matches && matches.length) {
count += matches.length;
if (count >= index) {
console.debug(node, tag, index, count, i);
return i;
}
}
}


return count;
});

// saved till end to avoid affecting source html
$('#source_pretty').html(html);
$('#source_raw').text(source);
$('#source_clean').text(clean_source.join('\n'));

$.each(line_numbers, function() { $('#line_number_' + this).css('background-color', 'orange'); });
},
});

var false_matches = [
"<div>",
"<div>",
"</div>",
"</div>"
].join('');

});
</script>
</head>
<!-- third comment -->
<body id="node2">
<div>
<pre id="source_pretty">
</pre>
<pre id="source_raw">
</pre>
<pre id="source_clean">
</pre>
</div>

<div id="node3">
<xmp>
<code>
// <xmp> is deprecated, you should put it in <code> instead
</code>
</xmp>
</div>

<!-- fourth comment -->
<div><div><div><div><div><div><span><div id="node4"><span><span><b><em>
<i><strong><pre></pre></strong></i><div><div id="node5"><div></div></div></div></em>
</b></span><span><span id="node6"></span></span></span></div></span></div></div></div></div></div></div>


<div>
<div>
<div id="node7">
<div>
<div>
<div id="node8">
<span>
<!-- fifth comment -->
<div>
<span>
<span>
<b>
<em id="node9">
<i>
<strong>
<pre>
</pre>
</strong>
</i>
<div>
<div>
<div>
</div>
</div>
</div>
</em>
</b>
</span>
<span>
<span id="node10">
</span>
</span>
</span>
</div>
</span>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>

关于javascript - 找出 dom 中的元素在 Javascript 中出现在哪个行号上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2044642/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com