<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>さくらたんどっとびーず &#187; jQuery プラグイン</title>
	<atom:link href="http://sakuratan.biz/archives/tag/jquery-%e3%83%97%e3%83%a9%e3%82%b0%e3%82%a4%e3%83%b3/feed" rel="self" type="application/rss+xml" />
	<link>http://sakuratan.biz</link>
	<description>モロモロ工事中です</description>
	<lastBuildDate>Sun, 25 Jun 2023 12:51:51 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>サラッとjQueryプラグインを書けると(･∀･)ｲｲ!!感じ</title>
		<link>http://sakuratan.biz/archives/1597</link>
		<comments>http://sakuratan.biz/archives/1597#comments</comments>
		<pubDate>Fri, 05 Feb 2010 08:53:57 +0000</pubDate>
		<dc:creator>さくら</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery プラグイン]]></category>

		<guid isPermaLink="false">http://sakuratan.biz/?p=1597</guid>
		<description><![CDATA[某サイトにて、type=&#8221;text&#8221; の input 要素を、こんな感じで jQuery を使って無効にしていたのですが、 function disable_enter(e) { if (e.which == 13) { return false; } return true; } $(function() { $('.noEnter').keypress(disable_... <div style="margin-top:1ex"><a href="http://sakuratan.biz/archives/1597">(続きを読む)</a></div>]]></description>
			<content:encoded><![CDATA[<p>某サイトにて、type=&#8221;text&#8221; の input 要素を、こんな感じで jQuery を使って無効にしていたのですが、</p>
<pre>
function disable_enter(e) {
    if (e.which == 13) {
       return false;
    }
    return true;
}

$(function() {
    $('.noEnter').keypress(disable_enter);
});
</pre>
<p>同じ機能を他でも使うことになって、いちいち keypress(disable_enter) 呼び出すのもうっとおしいなと思って、試しに jQuery プラグインにしてみたところ、応用範囲が超スゲー感じだったのでお伝えしたいと思います。</p>
<p style="margin-top:2em">とりま上のをプラグインにするとこんな感じ。</p>
<pre>
(function($) {
    $.fn.extend({
        disableEnter: function() {
            this.keypress(function(e) {
                return (e.which != 13);
            });
        }
    });
})(jQuery);

$(function() {
    $('.noEnter').disableEnter();
});
</pre>
<p>呼び出し側のコードが、超簡単になったのがお分かり頂けるでしょうか？</p>
<p style="margin-top:2em">jQuery プラグインの作り方は、プラグイン関数をメンバに持つオブジェクトを引数に渡すだけです。プラグイン関数内では、this は呼び出しに使用された jQuery オブジェクト（上の例なら $(&#8216;.noEnter&#8217;) が返すオブジェクト）になってますので、元々 jQuery 用に書かれたコードならだいたいそのまま動きます。</p>
<p style="margin-top:2em">元のコードでは「disable_enter を keypress イベントハンドラにセットするとエンター入力でフォームがサブミットされなくなります」という感じで、処理の概要の説明が若干まだるっこしいですが、プラグインにすると「disablEnter() を呼び出すとエンター入力でフォームがサブミットされなくなります」とう感じで、超スッキリします。</p>
<p>ちなみに $.fn.extend ではなく $.extend を使用すると、jQuery そのものの機能を拡張できるのですが、今日はﾒﾝﾄﾞｲので説明省略w</p>
<p style="margin-top:2em">jQuery には<a href="http://plugins.jquery.com/">プラグインディレクトリ</a> もありますので（たぶんエンターを無効にするプラグインはここにありそうw）、これ超イケテルプラグインができたらうｐしたら良いと思うのですが、それほど大したものじゃなくても、jQuery の処理をプラグインにしてパッケージングしておくと再利用性がぐーんと広がるような気がします。</p>
<p>ぜひお試しあれw</p>
]]></content:encoded>
			<wfw:commentRss>http://sakuratan.biz/archives/1597/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
