简体   繁体   中英

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.

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.

How do I get the native style attributes of <a> tags and apply them to my <span> tags?

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. The easier way would be to give both elements the same css class and style them accordingly.

This shouldn't be a nightmare as <a> and <span> tags are quite similar in the way they behave in the page flow.

Get the value of the target attribute of an <a> element:

var x = document.getElementById("myAnchor").getAttribute("target");

Get the value of the onclick event attribute of a <button> element:

var x = document.getElementById("myBtn").getAttribute("onclick");

Find out if a <button> element has an onclick attribute:

var x = document.getElementById("myBtn").hasAttribute("onclick");

Get the <a> element with id="myAnchor"

var x = document.getElementById("myAnchor"); 

If the <a> element has a target attribute, set the value to "_self"

if (x.hasAttribute("target")) {      
    x.setAttribute("target", "_self");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM