TA的每日心情 | 开心 2019-8-21 08:44 |
---|
签到天数: 163 天 [LV.7]常住居民III
|
发表于 2021-3-20 11:13:15
|
显示全部楼层
本帖最后由 mikeee 于 2021-3-20 11:31 编辑 5 S+ I% @7 [0 L1 {2 K+ P: R+ Q2 W
' O% \# u/ R+ `; S- <!DOCTYPE html>
; T: n6 z9 X# f M" m2 l - <html>" y5 C8 t$ Z! R, k+ e7 W% | a2 ~
- <body>% J* g# d6 _$ }/ D0 U
- / c% _7 Q) ~/ b6 }" t% D5 L
- <p>Click on the button to copy the text from the text field. Try to paste the text (e.g. ctrl+v) afterwards in a different window, to see the effect.</p>
* ^; s6 k5 D/ m - , a6 F) p( U$ e* j
- <div id="id1"> id1 text <button onclick="copyTextFromElement('id1')">Copy text</button> </div>
7 j/ c! b' |4 S k( H2 _ - 5 G3 V; y) V/ T3 U" Z/ F: o' h0 T; w
- <div id="id2"> id2 text2 <button onclick="copyTextFromElement('id2')">Copy text2</button> </div> " X7 S1 d5 P2 Y1 j ?
, Y- |5 ^0 }) C. B6 K- Q% S3 L- 0 x6 O8 U. Y' ^7 Z+ O! ^% y1 P
- <script>6 K, U# | f$ X6 J7 N( g7 `
- function copyTextFromElement(elementID) {
/ h9 g9 j9 ?6 k0 C Z: Z - let element = document.getElementById(elementID); //select the element
8 E6 y& @. w! z - let elementText = element.textContent; //get the text content from the element/ n" y' B! d+ h: a- G A+ H
- copyText(elementText); //use the copyText function below2 b3 }' k% _* V; u. A% @3 X
- }
5 F6 G: H1 a* b A - function copyText(text) {
3 \) i2 K, }; ]; [: R# h' k# b - navigator.clipboard.writeText(text);1 k0 T+ W, b2 z, e1 O& G
- }/ u' O1 @( o0 R5 S% ^+ I" i* |
- </script>
! N0 [8 C% ?7 Y" V' y( \
# K3 b( J/ o2 ^. T L. T- </body>
, Q/ e5 k* P. M1 R: [) P - </html>
复制代码
: m; Q& c- q3 J9 Y/ S5 o& g% G" I8 k) d2 g# x+ d6 d, x( u
参考 stackoverflow 的一个答案 https://stackoverflow.com/questi ... ipboard-not-working
& u$ T# o, b& N大致可以试试做成这样。我记得mdx以页为单位,所以每一页里都得放这个<script>...</script> . <div>那个标签也可以是别的标签例如<span> 什么的: X h& X- L* v
0 A) \2 z$ _/ E/ ]/ {9 x<button>...</button> 也可以放别的地方,只要里面的 id对上就可以了。0 Q2 e* w2 U2 s( i3 |
+ d1 m/ n& K0 `7 V可以先放一个 text-click.html 文件里用chrome或firefox浏览器测试,打开devtools(页面上右键选Inspect或F12)选Console查看可能出现的错误信息,对脚本做相应的修改。GoldenDict里好像也可以Inspect Element,mdict不太清楚。
+ c( z( D! z- _0 K7 u" v/ _: Z" h, w
) L1 r ?% ~/ |, S1 I或把上面的码拷到 https://www.w3schools.com/howto/ ... w_js_copy_clipboard 修改测试效果。 |
评分
-
2
查看全部评分
-
|