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

javascript - CSS Selector for name attribute of <a>? - Stack Overflow

programmeradmin1浏览0评论

This may be a dumb question, but what is the CSS Selector for the attribute of <a> that is "name"?

document.body.innerHTML = myString.anchor("HTML_String")

This JavaScript creates a <a> element with the name "HTML_String." How do I access only this element in my CSS?

This may be a dumb question, but what is the CSS Selector for the attribute of <a> that is "name"?

document.body.innerHTML = myString.anchor("HTML_String")

This JavaScript creates a <a> element with the name "HTML_String." How do I access only this element in my CSS?

Share Improve this question edited May 15, 2016 at 18:51 Shady Alset 5,7144 gold badges23 silver badges35 bronze badges asked May 15, 2016 at 18:46 Owen L.Owen L. 3461 gold badge4 silver badges7 bronze badges 3
  • Sorry, I meant "the attribute of <a> that is "name"? in the top line before the code. – Owen L. Commented May 15, 2016 at 18:46
  • 5 The name attribute for anchors has been removed from HTML as of HTML 5. Put an ID on the element you want to link to instead. – Quentin Commented May 15, 2016 at 18:55
  • @Quentin: Huh. So it has. I was under the impression it was simply deprecated. Edit: so it turns out when HTML5 says "obsolete" it can mean either "obsolete" or "deprecated" - checker.html5.org says that the name attribute is obsolete but it emits a warning, not an error unlike for things such as presentational attrs. – BoltClock Commented May 16, 2016 at 1:53
Add a comment  | 

4 Answers 4

Reset to default 7

CSS doesn't have a specific selector syntax for name attributes. You have to use the generic attribute selector syntax.

[att=val] Represents an element with the att attribute whose value is exactly "val".

There is the [name="name"] selector but it's not really cross-browser. Old versions of Internet Explorer don't support the selector by HTML attribute (that browser tho.........).

My suggestion is to always use classes for CSS (even for unique elements) and ids for JavaScript, while you'll leave the names for backend programming.

Add a class to the element and then a.myclass

use the [attribute=value] css selector, you can access it by using :

a[name=HTML_STRING] {
  //your css
}
[name="yourName"]

or if it only should have any name:

[name]

an example for an input:

input.myClass[name="greatName"] {
    ...
}

But if your question is how to look for something within your "innerHTML": This isn't possible.

发布评论

评论列表(0)

  1. 暂无评论