Home Web Front-end JS Tutorial Copy content to clipboard compatible browser with js_javascript tips

Copy content to clipboard compatible browser with js_javascript tips

May 16, 2016 pm 04:55 PM
clipboard copy

If you want to copy content to the clipboard through js, it is not difficult at first. However, if you consider the browser compatibility issue, it becomes a bit troublesome. Using jquery-zclip to copy is a good choice. Use flash to achieve browser compatibility. . I won’t go into details about the principle, but let’s talk about how to implement it.

For example, my html code is as follows:

Copy the code The code is as follows:


<span id="id_1">Content to be copied 1</span>
<span id="id_2">Content to be copied 2</span>
< span id="id_3">Content to be copied 3</span>


A total of two js files are required, not to mention jquery , and then there are jquery-zclip.js and ZeroClipboard.swf. These two files can be downloaded on the official website at the following address: http://www.steamdev.com/zclip/

Generate copy button js is as follows
Copy code The code is as follows:



This enables cross-browser copying. It’s not a difficult thing to do. It went smoothly when I tested it. But when I put it into the project, something went wrong. There was no flash where the copy button was generated. , only text. Later, I found out that the flash was actually generated, but not where the text was. It may have something to do with the iframe structure used in the background of my project. I guess this is a bug of this plug-in. After checking a lot of information, there was People said that the code needs to be changed, and then I changed it and it worked.

The code that needs to be modified is as follows
Copy Code The code is as follows:

getDOMObjectPosition: function (obj, stopObj) {
// get absolute coordinates for dom element
var info = {
left: 0,
top: 0,
width: obj.width ? obj.width : obj.offsetWidth,
height: obj.height ? obj.height : obj.offsetHeight
} ;

if (obj && (obj != stopObj)) {
//info.left = obj.offsetLeft; //Before modification
//info.top = obj.offsetTop; / /Before modification
jpos = $(obj).position(); //After modification
info.left = jpos.left; //After modification
info.top = jpos.top; //After modification After
}

return info;
}

Actually, this is related to the principle of this plug-in. He covers the button with a transparent flash. If the two of them do not overlap, the above problem will arise.
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to view clipboard history on Mac How to view clipboard history on Mac Sep 14, 2023 pm 12:09 PM

View macOS Clipboard History from Finder When you copy anything on your Mac (text, images, files, URLs, etc.), it goes to the Clipboard. Here's how to see what was last copied on your Mac. Go to Finder and click "Edit" in the menu bar. Click "Show Clipboard". This will open a window showing the contents of the macOS clipboard. Get Clipboard History Containing Multiple Items in Mac Of course, native Mac Clipboard History isn't the most versatile tool you can have. It only shows the last thing you copied, so if you want to copy multiple things at once and then paste them all together, you simply can't do that. However, if you are looking for

How to solve the problem of Windows + Shift + S not working properly? How to solve the problem of Windows + Shift + S not working properly? May 08, 2023 pm 07:16 PM

Check your keyboard If keyboard shortcuts don't work, there may be an issue with the keyboard itself. Make sure it is plugged in correctly and recognized by your PC. Have a laptop keyboard giving you trouble? If you have an extra keyboard, plug it in and see if it works. If so, it may be an issue with the keyboard itself. Using a wireless keyboard? Follow the manufacturer's instructions to re-pair. You should also check any cables for damage and make sure the keys are free of debris and pressed properly. For more information, check out these fixes for broken Windows 11 keyboard. Using the Print Screen Key If you desperately need a screenshot and don't have time to troubleshoot, you can use the workaround first. For a full photo of the desktop, click

Clipboard on iPhone: How to access it? Clipboard on iPhone: How to access it? May 16, 2023 am 10:46 AM

Where is the clipboard on iPhone? Since iOS is a closed ecosystem, the clipboard is an internal feature stored in virtual memory. By default, users cannot view the clipboard contents or edit them in any way. However, there are some ways to access the clipboard on iPhone. You can paste the clipboard into the Notes application (or any other text editor) and keep multiple copies of the clipboard there, editing them as needed. Alternatively, you can use the Shortcuts app to create a shortcut that displays the clipboard. Finally, you can use a third-party application such as Paste. How to access the clipboard on iPhone? Whether you prefer to use the built-in Notes app,

How to fix Windows 11 copy and paste function unresponsive issue? How to fix Windows 11 copy and paste function unresponsive issue? Apr 26, 2023 pm 08:55 PM

Copying has always been a great way to save and share files. It allows you to create manual backups, perform simple transfers, and even make modifications that might damage the original file if not copied. However, this can sometimes be difficult if you have problems trying to use the clipboard on your Windows PC. Some Windows 11 users have recently been unable to use copy-paste on their systems. This issue can occur due to a number of reasons and if you are in the same boat, then you can fix it on your PC. How to Fix Copy-Paste on Windows 11 Since there are many issues that can cause copy-paste to malfunction on Windows, we have listed the most common solutions to fix this problem below. We recommend that you start with the first

How to copy lyrics from QQ Music How to copy lyrics How to copy lyrics from QQ Music How to copy lyrics Mar 12, 2024 pm 08:22 PM

We users should be able to understand the diversity of some functions when using this platform. We know that the lyrics of some songs are very well written. Sometimes we even listen to it several times and feel that the meaning is very profound. So if we want to understand the meaning of it, we want to copy it directly and use it as copywriting. However, if we want to use it, we still need to You just need to learn how to copy lyrics. I believe that everyone is familiar with these operations, but it is indeed a bit difficult to operate on a mobile phone. So in order to give you a better understanding, today the editor is here to help you. A good explanation of some of the above operating experiences. If you also like it, come and take a look with the editor. Don’t miss it.​

How to use clipboard history on Windows 11 How to use clipboard history on Windows 11 Apr 19, 2023 pm 12:13 PM

&lt;h2&gt;Using Clipboard History on Windows 11&lt;/h2&gt;&lt;p&gt;You can still use &lt;strong&gt;right-click&gt;copy&lt;/strong&gt; and &lt;strong&gt;right-click&gt; Paste &lt;/strong&gt; or use &lt;strong&gt;Ctrl+C&lt;/strong&amp

Screenshots and screenshot location on Windows 11/10 Screenshots and screenshot location on Windows 11/10 May 16, 2023 pm 04:01 PM

Where are clippings and screenshots in Windows 11? This is a question we get from some of our readers who may have just installed a new operating system or used a snipping tool for the first time. This tool is designed to capture any or all parts of your computer screen. To put things in context, the PrintScreen key will take a complete snapshot of your screen, but the SnippingTool can be adjusted to grab just the area you like. Where are screenshots and screenshots on Windows 10/11? By default, clippings and screenshots are saved to your computer's clipboard. This means that to retrieve them you simply paste them into your desired application such as Microsoft Paint, Photo

What is the shortcut key for copying? What is the shortcut key for copying? Mar 10, 2023 pm 02:00 PM

The shortcut key for copying is "Ctrl+c", and the corresponding paste key is "Ctrl+v"; on the computer, use the mouse to drag and select text, hold down Ctrl, and then click the C key to complete the copy; A shortcut key refers to completing an operation through certain specific keys, key sequences, or key combinations.

See all articles