[英]style one element with the native css of another
I want to achieve a paragraph of text where some words are styled as <a>
tags with the native styling of the browser. 我想实现一段文本,其中某些单词将使用浏览器的本机样式设置为<a>
标记。
These words might really be in an <a>
tag with an href attribute to direct the user to another url, or they might be in a disguised <span>
tag with a js onclick function. 这些单词实际上可能在带有href属性的<a>
标记中,以将用户定向到另一个URL,或者它们可能在带有js onclick函数的伪装的<span>
标记中。
How do I get the native style attributes of <a>
tags and apply them to my <span>
tags? 如何获取<a>
标签的本机样式属性并将其应用于我的<span>
标签?
It's simple: You don't. 很简单:您不需要。
You can extend an element's properties with SASS and LESS but not the native ones from a certain tag. 您可以使用SASS和LESS扩展元素的属性,但不能扩展特定标签的本机属性。 The easier way would be to give both elements the same css class and style them accordingly. 更简单的方法是为两个元素提供相同的CSS类,并相应地设置其样式。
This shouldn't be a nightmare as <a>
and <span>
tags are quite similar in the way they behave in the page flow. 这不应该是一场噩梦,因为<a>
和<span>
标记在页面流中的行为非常相似。
Get the value of the target attribute of an <a>
element: 获取<a>
元素的target属性的值:
var x = document.getElementById("myAnchor").getAttribute("target");
Get the value of the onclick event attribute of a <button>
element: 获取<button>
元素的onclick事件属性的值:
var x = document.getElementById("myBtn").getAttribute("onclick");
Find out if a <button>
element has an onclick attribute: 找出<button>
元素是否具有onclick属性:
var x = document.getElementById("myBtn").hasAttribute("onclick");
Get the <a>
element with id="myAnchor"
使用id="myAnchor"
获取<a>
元素
var x = document.getElementById("myAnchor");
If the <a>
element has a target attribute, set the value to "_self"
如果<a>
元素具有目标属性,则将该值设置为"_self"
if (x.hasAttribute("target")) {
x.setAttribute("target", "_self");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.