Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

javascript - Copy To Clipboard Event Listener on CSS Pseudo Element

I have several code snippets on my page, none of which have names or IDs.

<code>code section 1</code>
<code>code section 2</code>

I am using a CSS pseudo-element to automatically place a 'copy' icon after each code snippet.

code::after {
    content: "copy-icon";
    margin-left: 10px;
    cursor: pointer;
}

I want the text inside each code snippet to be copied when the icon next to it is clicked. I am having difficulty achieving this because I'm not able to reference the code snippets, or the icons, because neither have names or IDs.

Can anybody help me with the following:

  1. Ensure the correct text is copied - depending on which icon is clicked.

  2. Apply the event listener to the icons only, not the code snippets themselves.

I have attempted a JS Fiddle. I am quite stuck and could use some help.

https://jsfiddle.net/DekuDigital/zenymrav/

Thanks Antony

EDIT 1

I have tried to select the first pseudo-element as follows, but not working:

document.getElementsByTagName("code")[0], ':after'

question from:https://stackoverflow.com/questions/65830557/copy-to-clipboard-event-listener-on-css-pseudo-element

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...