最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Select element by content using jQuery - Stack Overflow

programmeradmin1浏览0评论

I am currently building an application that requires the identification of an element by its content. Normally I would use the ID of an element to identify it, but in this case it is not possible since the content is user generated.

I have a PHP script that outputs some content, part of which is user entered. For example:

<!-- elementid1 -->
This is user generated text. Lorem ipsum...

I need to be able to identify this element and fire an event on a click. Once the click happens, I should be able to parse the ID (elementid1) from the content easily.

However, these elements aren't always just DIVs. Sometimes they are H1 through H6, etc. How would I go about detecting these events

OR

Alternatively, I could loop through all elements (the part I don't know how to do), setting a custom attribute with the ID, then creating jQuery events for elements with the attribute equalling something. If anyone has insight on how to loop through every DOM element (preferably only the children, since the parents would have the content of the children as well...) then this would also work.

A Note:

Normally I would use the contains() selector, but since this ID is a ment and not text it doesn't seem to work.

I am currently building an application that requires the identification of an element by its content. Normally I would use the ID of an element to identify it, but in this case it is not possible since the content is user generated.

I have a PHP script that outputs some content, part of which is user entered. For example:

<!-- elementid1 -->
This is user generated text. Lorem ipsum...

I need to be able to identify this element and fire an event on a click. Once the click happens, I should be able to parse the ID (elementid1) from the content easily.

However, these elements aren't always just DIVs. Sometimes they are H1 through H6, etc. How would I go about detecting these events

OR

Alternatively, I could loop through all elements (the part I don't know how to do), setting a custom attribute with the ID, then creating jQuery events for elements with the attribute equalling something. If anyone has insight on how to loop through every DOM element (preferably only the children, since the parents would have the content of the children as well...) then this would also work.

A Note:

Normally I would use the contains() selector, but since this ID is a ment and not text it doesn't seem to work.

Share Improve this question edited Dec 13, 2022 at 9:40 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Feb 22, 2011 at 6:25 Andrew MAndrew M 4,28811 gold badges44 silver badges68 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

Try this...

$('*').filter(function() {
   return $(this).html().match('<!-- elementid1 -->');
});

jsFiddle.

You can create custom selector which will search the children with nodeType = COMMENT_NODE (8) and specific content ... But this is a long time...

jsFiddle

发布评论

评论列表(0)

  1. 暂无评论