admin管理员组文章数量:1023848
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.
2 Answers
Reset to default 8Try 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
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.
2 Answers
Reset to default 8Try 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
本文标签: javascriptSelect element by content using jQueryStack Overflow
版权声明:本文标题:javascript - Select element by content using jQuery - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745513127a2153916.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论