查找HTML元素的行号 [英] Finding line numbers of HTML elements

查看:99
本文介绍了查找HTML元素的行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在帮助开发一个验证给定HTML元素的开发人员工具

有一个给定的属性(例如,所有LABEL元素具有FOR

属性,所有INPUT元素都具有ID属性等。几乎

所有功能都有效,除了在

中显示违反用户设置规则的HTML行的哪一项功能发生

(例如,LABEL元素没有FOR属性的地方)。在那里,用bavascript来查找

这个页面的一段HTML代码是不是一个简单明了的方式,所以我'已经通过Mozilla Firefox的Javascript

控制台的XBL / XPCOM源代码阅读了

,以获得一些见解。虽然我正在慢慢地用这个

方法取得进展,但我想知道我是不是忽略了用Javascript来做我想要的b $ b想要的东西。有没有人处理过这类问题?


谢谢,

Eric

解决方案

e271828在8/13/2006 6:18 PM声明如下:





我正在帮助开发一个验证给定HTML元素的开发人员工具

有一个给定的属性(例如,所有LABEL元素都有一个FOR

属性,所有INPUT元素都有ID属性等)。几乎

所有功能都有效,除了在

中显示违反用户设置规则的HTML行的哪一项功能发生

(例如,LABEL元素没有FOR属性的地方)。在那里,用bavascript来查找

这个页面的一段HTML代码是不是一个简单明了的方式,所以我'已经通过Mozilla Firefox的Javascript

控制台的XBL / XPCOM源代码阅读了

,以获得一些见解。虽然我正在慢慢地用这个

方法取得进展,但我想知道我是不是忽略了用Javascript来做我想要的b $ b想要的东西。有人处理过这种问题吗?



如何找到这个行号取决于你如何阅读来源。

但是,你总是可以读取整个页面字符串,找到有问题的代码发生的
,从0到
得到一个子字符串的违规代码,然后将该子字符串拆分为\\\\ nn和结果数组减去1的长度将是你的行号。


-

兰迪

comp.lang.javascript常见问题 - http://jibbering.com/faq &安培;新闻组每周

Javascript最佳实践 - http://www.JavascriptToolbox .com / bestpractices /


我最初也想做这些事情,但是

以下Javascript片段我试过的工作做得不对:


alert("''" + document.toString()+"''");返回[objectHTMLelement] -

绝对不是我想要的。将.length方法应用于

,返回document.toString()中的字符数(21)

结果。我也试过提醒(''" +

document.getElementsByTagName(" html")。toString()+"''"和

文件.getElementsByTagName(" html")。innerHTML +"''");,但也没有

运气。如果没有办法将文件作为

字符串,那么就没有办法继续了。我想我可能会尝试扩展Firefox的Javascript调试器XBL绑定(这对于一个相当普通的功能而言,b / b
感觉太多了),除非有人

还有其他想法。


感谢您提供的意见。


Randy Webb写道:
< blockquote class =post_quotes>
e271828在8/13/2006 6:18 PM声明如下:





我正在帮助开发一个验证给定HTML元素的开发人员工具

有一个给定的属性(例如,所有LABEL元素都有一个FOR

属性,所有INPUT元素都有ID属性等)。几乎

所有功能都有效,除了在

中显示违反用户设置规则的HTML行的哪一项功能发生

(例如,LABEL元素没有FOR属性的地方)。在那里,用bavascript来查找

这个页面的一段HTML代码是不是一个简单明了的方式,所以我'已经通过Mozilla Firefox的Javascript

控制台的XBL / XPCOM源代码阅读了

,以获得一些见解。虽然我正在慢慢地用这个

方法取得进展,但我想知道我是不是忽略了用Javascript来做我想要的b $ b想要的东西。有人处理过这种问题吗?



如何找到这个行号取决于你如何阅读来源。

但是,你总是可以把整个页面读作一个字符串,找到有问题的代码发生的
,从0到
得到一个子字符串的违规代码,然后将该子字符串拆分为\\\\ nn和结果数组减去1的长度将是你的行号。


-

兰迪

comp.lang.javascript常见问题 - http://jibbering.com/faq &安培;新闻组每周

Javascript最佳实践 - http://www.JavascriptToolbox .com / bestpractices /


我知道了。为了将来参考,获取页面的innerHTML源的一种方法是使用方法document.documentElement.innerHTML。由于

显然在页面呈现后没有回车,因此按行号拆分HTML源的最佳方法是

将document.documentElement.innerHTML.split(" \ n")放到最后。


感谢Randy的建议。我将不得不在Mozilla的XBL / XPCOM推迟我的自杀

再次进行。


-Eric


Hi,

I''m helping to work a developer tool that verifies a given HTML element
has a given attribute (e.g., that all LABEL elements have a FOR
attribute, all INPUT elements have an ID attribute, etc.). Pretty much
all of the functionality is working except a feature that shows in
which line of the HTML source a violation of the user-set rule occurs
(e.g., where a LABEL element doesn''t have a FOR attribute). There
doesn''t seem to be a straightforward way with Javascript to find out at
which line a piece of a page''s HTML code is, so I''ve been reading
through the XBL/XPCOM source code of Mozilla Firefox''s Javascript
Console for some insight. While I''m slowly making progress with that
method, I was wondering if I haven''t overlooked some way to do what I
want with Javascript. Has anyone dealt with this kind of problem?

Thanks,
Eric

解决方案

e271828 said the following on 8/13/2006 6:18 PM:

Hi,

I''m helping to work a developer tool that verifies a given HTML element
has a given attribute (e.g., that all LABEL elements have a FOR
attribute, all INPUT elements have an ID attribute, etc.). Pretty much
all of the functionality is working except a feature that shows in
which line of the HTML source a violation of the user-set rule occurs
(e.g., where a LABEL element doesn''t have a FOR attribute). There
doesn''t seem to be a straightforward way with Javascript to find out at
which line a piece of a page''s HTML code is, so I''ve been reading
through the XBL/XPCOM source code of Mozilla Firefox''s Javascript
Console for some insight. While I''m slowly making progress with that
method, I was wondering if I haven''t overlooked some way to do what I
want with Javascript. Has anyone dealt with this kind of problem?

How you would find that line number depends on how you read the source.
But, you could always read the entire page as a string, find the
occurrence of the offending code, get a substring from 0 to the point of
the offending code, then split that substring on \r\n and the length of
the resulting array minus one will be your line number.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


I intially thougt of doing something along those lines as well, but the
following pieces of Javascript that I tried didn''t work quite right:

alert("''" + document.toString() + "''"); returns [objectHTMLelement] --
definitely not what I''m looking for. Applying the .length method to
that returns the number of characters (21) in the document.toString()
result. I also tried alert("''" +
document.getElementsByTagName("html").toString() + "''" and
document.getElementsByTagName("html").innerHTML + "''");, but had no
luck there either. If there isn''t a way to get the document as a
string, then there''s little option to proceed. I think I might try my
hand at extending Firefox''s Javascript Debuggers XBL bindings (which
feels like too much work for a fairly mundane feature), unless anybody
has other ideas.

Thanks for the input though.

Randy Webb wrote:

e271828 said the following on 8/13/2006 6:18 PM:

Hi,

I''m helping to work a developer tool that verifies a given HTML element
has a given attribute (e.g., that all LABEL elements have a FOR
attribute, all INPUT elements have an ID attribute, etc.). Pretty much
all of the functionality is working except a feature that shows in
which line of the HTML source a violation of the user-set rule occurs
(e.g., where a LABEL element doesn''t have a FOR attribute). There
doesn''t seem to be a straightforward way with Javascript to find out at
which line a piece of a page''s HTML code is, so I''ve been reading
through the XBL/XPCOM source code of Mozilla Firefox''s Javascript
Console for some insight. While I''m slowly making progress with that
method, I was wondering if I haven''t overlooked some way to do what I
want with Javascript. Has anyone dealt with this kind of problem?


How you would find that line number depends on how you read the source.
But, you could always read the entire page as a string, find the
occurrence of the offending code, get a substring from 0 to the point of
the offending code, then split that substring on \r\n and the length of
the resulting array minus one will be your line number.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


I got it. For future reference, one way to get the innerHTML source of
a page is to use the method document.documentElement.innerHTML . Since
there apparently aren''t carriage returns after the page has been
rendered, the best way to split the HTML source by line number is to
put a document.documentElement.innerHTML.split("\n") onto the end.

Thanks for the suggestions Randy. I will have to postpone my suicidal
foray in Mozilla''s XBL/XPCOM for another day.

-Eric


这篇关于查找HTML元素的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆