简体   繁体   English

用另一个元素的本机CSS样式一个元素

[英]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. 您可以使用SASSLESS扩展元素的属性,但不能扩展特定标签的本机属性。 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.

相关问题 使用JavaScript,如何根据另一个元素的CSS样式更改一个元素的CSS样式? - Using JavaScript, how do I change the CSS style of one element based on the CSS style of another element? 将CSS样式从一个元素复制到单独文档中的新元素 - Copy CSS style from one element to a new element in a separate document 在React Native中将样式值从文件传递到另一个 - Pass a style value from a file to another one in react native jQuery将一个元素的css值设置为另一个 - jQuery to set css value of one element to another 如何 select css 变量并将样式值分配给另一个元素 - How to select css variable and assign style value to another element 在 js 中创建元素然后在另一个 css 文件中应用样式 - create element in js then apply style in another css file 有没有办法 hover 一个元素并使用纯 css/js 设置许多其他元素的样式? - Is there a way to hover one element and to style many others with pure css/js? 如何在JavaScript中将CSS样式从一页更新到另一页 - How to update css style from one page to another in JavaScript 如何将所有 CSS 样式从一个 DOM 复制到另一个? - How to copy all CSS style from one DOM to another? 将 javascript 计算样式从一个元素设置/复制到另一个元素 - Set / Copy javascript computed style from one element to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM