星期一, 十二月 29, 2008

jQuery 點擊 Input / Textarea 全選、複製的寫法(Widget)

最近寫不少 Widget / Badge / 外掛工具, 順便做點小筆記.

首先要寫外掛工具, 就會有個 Input / Textarea 的欄位, 裡面放要讓人嵌入的程式碼, 然後旁邊要有個按紐, 是可以點擊就可以自動將程式碼複製進剪貼簿的功能, 所以下述會有幾個步驟:

  1. 點擊 Input / Textarea 全選
  2. 設定 Input / Textarea 裡面的值不能被修改
  3. 點選複製功能
  4. IE/Firefox 有異常的處理
  5. 完整 jQuery 程式碼

HTML 區塊範例

<div id="badge">
    <input class="badge-code" type="text" value=".... badge-code ....">
    <input class="badge-code" type="text" value=".... badge-code ...." readonly>
    <input class="badge-code-hidden" type="hidden" value=".... badge-code ....">
    <!-- badge-code-hidden 下面會說明要做何用途 -->
    <a id="copy-embed-code" href="#">複製程式碼</a>
</div><!-- #badge -->

點擊 Input / Textarea 全選

$("#badge .badge-code").click(function(){
    this.select();
});

設定 Input / Textarea 裡面的值不能被修改

註: 設定 input readonly 的話, 此段就不需要了.

在上面有看到此段 <input class="badge-code-hidden" type="hidden" value=".... badge-code ....">, 此行的 badge code 和上面秀出來的都一樣, 為的就是這行不會被使用者改到, 若上述的 Input / Textarea 欄位, 在 Javascript 被執行前, 就被使用者改到, 反而會讓使用者拿不到正確的 Widget code.

var copydata = $("#badge .badge-code-hidden").val(); // 先取得 hidden 正確的 Widget code value.
/* 使用者若有對此欄位做任何修改, 都將它恢復成原始預設值 */
$("#badge .badge-code").bind("keydown keypress keyup", function(e){
    $("#badge .badge-code").val(copydata);
    this.select();
});

點選複製功能

在上面有看到 複製程式碼 的 HTML 區塊, 此時需要此 Guapo - jQuery copy plugin, 它會透過 Flash 幫我們將值複製到剪貼簿.

  1. 下載 Guapo - jQuery copy plugin - jquery.copy.js, copy.swf
  2. 加入此段 <script type="text/javascript" src="jquery.copy.js"></script>
  3. 程式碼加入此段
    $("#copy-embed-code").click(function(){
        alert('程式碼已複製');
        $.copy($("#badge .badge-code").val());
    });

IE/Firefox 有異常的處理

此 jQuery copy plugin 在 Firefox 3.x 版早期前都是可以用的, 但是現在最新的 Firefox 不支援, 所以就讓此自動複製的功能, 只在 IE 秀出來, Firefox 不秀, 於 CSS 做下述設定:

style.css
#copy-embed-code {display:none;}
設定 IE 才看的到此段 css - ie-style.css
<!--[if IE]>
<link rel="stylesheet" href="ie-style.css" type="text/css">
<![endif]-->
ie-style.css 內容
#copy-embed-code {display:inline;}

完整 jQuery 程式碼

/* 全選 */
$("#badge .badge-code").click(function(){
    this.select();
});

/* 禁止修改, 如果設定 readonly, 此段則可省略 */
var copydata = $("#badge .badge-code-hidden").val();
$("#badge .badge-code").bind("keydown keypress keyup", function(e){
    $("#badge .badge-code").val(copydata);
    this.select();
});

/* 複製 */
$("#copy-embed-code").click(function(){
    alert('程式碼已複製');
    $.copy($("#badge .badge-code").val());
});

相關標籤

this is comment icon readonly [回覆]

input type=readonly 是否就可以避免使用者更改了?

Comment by fauzty (12/29/2008 03:17)

this is comment icon 回 fauzty [回覆]

感恩, 我都忘了有這招了.. Orz..
不過順便修正個小錯誤, input type="text" readonly 這樣子就可以了, type="readonly" 沒有用~ :)

Comment by Tsung (12/29/2008 13:08)

this is comment icon 全選 [回覆]

你好
不用透過全選好像也可以複製喔 :]

Comment by feka (01/25/2009 06:47)

this is comment icon 回 feka [回覆]

嗯嗯, IE 是可以不用全選複製, 但是 Firefox 目前沒有辦法複製, 所以為了讓使用者方便點, 直接按下去先全選, 再讓他自行複製應該會比較好~ :)

Comment by Tsung (01/25/2009 12:05)

this is comment icon [回覆]

FF也可以複製喔
請參考~
http://fekasource.googlepages.com/copy.html

Comment by feka (01/25/2009 17:51)

this is comment icon 回 feka [回覆]

我試不行耶, 我之前試是可以的, 但是在某次升級後, 好像在 FF 3.x 版以上, 就不能 copy 了. (您提供的網址, 我在 FF 還是不能 copy 耶)

Comment by Tsung (01/25/2009 18:02)

this is comment icon [回覆]

這就怪了…
我是用3.0.5的
一切正常

Comment by feka (01/25/2009 22:14)

this is comment icon 回 feka [回覆]

奇怪, 我找人也是 3.0.5, 不能用耶.. Orz..
我找了兩個人試過, 都不行耶.

Comment by Tsung (01/25/2009 23:23)

this is comment icon [回覆]

抱歉啊>"<
我把暫存刪掉後也不行了
因為之前用txt.value寫
忘了給form name
現在應該可以了
可以再試試嗎…

如果還是不行…就真的很奇怪了啊XD

Comment by feka (01/26/2009 00:22)

this is comment icon 回 feka [回覆]

我找兩個人測過, 還是不行耶. Orz..

Comment by Tsung (01/26/2009 00:45)

this is comment icon [回覆]

那要如何知道使用者目前是否是在用 IE 呢??

Comment by 無 (12/30/2009 15:05)

this is comment icon 回 無 [回覆]

這邊 http://docs.jquery.com/Utilities/jQuery.browser , 可以知道使用者用的瀏覽器是哪一種~ :)

Comment by Tsung (12/30/2009 15:17)

this is comment icon 大大可以和交個朋友交流一下嗎? [回覆]

大大可以和交個朋友交流一下嗎? 有些問題不太好描述的 我的MSN是thor777@seed.net.tw
Comment by Arthas (12/31/2009 14:15)

this is comment icon 回 Arthas [回覆]

jon

喔喔, 可以阿. 只是要不要先 Email 描述一下?

Comment by Tsung (01/01/2010 12:41)

this is comment icon 這兩天遇到了一個怪問題(CSS) [回覆]

http://twpug.net/modules/newbb/viewtopic.php?topic_id=4814&post_id=17897#forumpost17897

討論串是我發文的,
關於css見鬼的問題~~

Comment by Arthas (01/05/2010 23:40)

this is comment icon 回 Arthas [回覆]

style 裡面的註解, 是用 /* */, 不是用 html 註解. 把你那個註解 拿掉就可以了..

Comment by Tsung (01/05/2010 23:50)

this is comment icon [回覆]

大大想請教一下有什麼比較推薦的模組嗎?
畫圓餅圖,柱狀圖之類的數據~

Comment by Arthas (01/08/2010 12:09)

this is comment icon 回 Arthas [回覆]

Google Chart. XD
http://code.google.com/intl/zh-TW/apis/chart/

Comment by Tsung (01/08/2010 12:28)

this is comment icon [回覆]

mssql和mysql的資料庫可以互相轉換嗎??
好奇問一下,
可以的話要用什麼轉??

Comment by 不好意思又來了 (01/12/2010 14:54)

this is comment icon 回 不好意思又來了 [回覆]

這個我也不知道耶, 可能得要 select + insert, 再不然就是要 parse mssql 的格式了.
mssql 倒出來的格式需要額外 parse 一下.

Comment by Tsung (01/12/2010 15:18)
Add this page to del.icio.us

發表迴響

標題

內容 (限制 1000 字)

暱稱

電子郵件

個人網頁


 authimage


PS: 若無法留言, 請先確認是否有打開 JavaScript, 造成您的困擾, 實在萬分對不起 Orz...(如果無法留言, 勞煩可以發信給我好嗎? 謝謝.)
PS2: 若您的留言被誤判, 我都會再自行看過, 不需要一直重覆張貼~