selectors-api

Javascript .querySelector find <div> by innerTEXT

醉酒当歌 提交于 2019-11-26 17:27:57
How can I find DIV with certain text? For example: <div> SomeText, text continues. </div> Trying to use something like this: var text = document.querySelector('div[SomeText*]').innerTEXT; alert(text); But ofcourse it will not work. How can I do it? gdyrrahitis OP's question is about plain JavaScript and not jQuery . Although there are plenty of answers and I like @Pawan Nogariya answer, please check this alternative out. You can use XPATH in JavaScript. More info on the MDN article here . The document.evaluate() method evaluates an XPATH query/expression. So you can pass XPATH expressions

Using querySelector with IDs that are numbers

二次信任 提交于 2019-11-26 17:25:24
From what I understand the HTML5 spec lets you use IDs that are numbers like this. <div id="1"></div> <div id="2"></div> I can access these fine using getElementById but not with querySelector . If I try do the following I get SyntaxError: DOM Exception 12 in the console. document.querySelector("#1") I'm just curious why using numbers as IDs does not work querySelector when the HTML5 spec says these are valid. I tried multiple browsers. Dennis It is valid, but requires some special handling. From here: http://mathiasbynens.be/notes/css-escapes Leading digits If the first character of an

.parents() without jquery - or querySelectorAll for parents [duplicate]

*爱你&永不变心* 提交于 2019-11-26 14:08:44
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Check event.target.parentElement with matchesSelector js I have a dom object, I'd like to match its parents, all parents, against a selector, like querySelectAll() , but for parents instead of children. Similar to jQuery's .parents('selector') method, but I do NOT need any back-wards compatibility. Also, no libraries please. I will totes take a boolean return value. I CAN write this myself as a recursive

querySelector search immediate children

六眼飞鱼酱① 提交于 2019-11-26 12:57:53
问题 I have some jquery-like function: function(elem) { return $(\'> someselector\', elem); }; The question is how can i do the same with querySelector() ? The problem is > selector in querySelector() requires parent to be explicitly specified. Is there any workaround? 回答1: Though it's not a full answer, you should keep an eye on the W3C Selector API v.2 which is already available in Google Chrome and Safari 7.x (both desktop and mobile), but as far as I have tested, still not there in Firefox and

Can I use a regular expression in querySelectorAll?

蓝咒 提交于 2019-11-26 11:00:53
问题 On a page I\'m doing I will be ending up with custom link elements like this: <link rel=\"multiply\" type=\"service/math\" src=\"path/to/service\"> <link rel=\"substract\" type=\"service/math\" src=\"path/to/service\"> ... I\'m trying to use querySelectorAll to retrieve all link elements with a type service/... specified and am getting nowhere. Currently I\'m selecting this: root.querySelectorAll(\'link\'); which gives me all <link> elements when I only want the ones with type service/.*

IE8 does not support querySelectorAll

我怕爱的太早我们不能终老 提交于 2019-11-26 09:35:57
问题 I tried to use document.querySelectorAll() , but IE8 throw error, that Object doesn\'t support this property or method var titleCheckBox = document.querySelectorAll(\"\"); Here http://www.quirksmode.org/dom/w3c_core.html#t13 written, that IE8 support this method. What I doing wrong? 回答1: Check that your page isn't in Quirks mode or Compatibility mode. You can use the F12 dev tools to confirm this. Press F12 and look in the top-right corner of the resulting window. If you see "Compatibility"

Javascript .querySelector find <div> by innerTEXT

谁都会走 提交于 2019-11-26 05:24:28
问题 How can I find DIV with certain text? For example: <div> SomeText, text continues. </div> Trying to use something like this: var text = document.querySelector(\'div[SomeText*]\').innerTEXT; alert(text); But ofcourse it will not work. How can I do it? 回答1: OP's question is about plain JavaScript and not jQuery . Although there are plenty of answers and I like @Pawan Nogariya answer, please check this alternative out. You can use XPATH in JavaScript. More info on the MDN article here. The

Changing CSS pseudo-element styles via JavaScript

五迷三道 提交于 2019-11-26 00:43:52
问题 Is it possible to change a CSS pseudo-element style via JavaScript? For example, I want to dynamically set the color of the scrollbar like so: document.querySelector(\"#editor::-webkit-scrollbar-thumb:vertical\").style.background = localStorage.getItem(\"Color\"); and I also want to be able to tell the scrollbar to hide like so: document.querySelector(\"#editor::-webkit-scrollbar\").style.visibility = \"hidden\"; Both of these scripts, however, return: Uncaught TypeError: Cannot read property