は私が$("myDiv1")
と$("myDiv2")
のような2つのjQueryの要素を持っており、それがSetText()
と呼ばれるために私がprotoを定義し,, SetText
実装で ? あなたはthis
を使用し、
答えて
ありがとう:
- あなたのプラグインのコード内では、それはあなたのプラグインが、それはDOM要素を指し、イベントハンドラと、このような内
- で呼び出されたオブジェクトのjQueryを指します
オプションを持つ単純なプラグインやメソッドのカップルのクイック例:
// Our "nifty" plugin.
// Note this is an ES5 and earlier example, no ES2015+ features used.
(function($) {
// Convenient access to Array#slice
var slice = Array.prototype.slice;
// Our event namespace
var eventNS = ".nifty";
// Our data name for options
var optionsName = "options.nifty";
// Some default options
var niftyDefaults = {
color: "green",
text: "default text"
};
// Our methods
var methods = {
init: nifty$init,
destroy: nifty$destroy,
options: nifty$options,
settext: nifty$settext
};
// Main plugin function
$.fn.nifty = function(options) {
// Note that here, `this` is the jQuery object our plugin was called on.
var method, args;
// Determine the method to call and arguments to it
if (typeof options === "string") {
// Method call
method = options;
args = slice.call(arguments, 1); // 1 = skip the method name
} else {
// Init
method = "init";
args = slice.call(arguments, 0); // 0 = copy all
}
// Call the method, return the result
return methods[method].apply(this, args);
};
// Because of the way we call them, `this` in all of our methods below
// is the jQuery object we were called on
// Init
function nifty$init(options) {
// Initialize each element individually
this.each(function() {
// Note that here, `this` is the DOM element for this iteration of
// the `each` loop. Save the options; each element gets its own copy.
$(this).data(optionsName, $.extend({}, niftyDefaults, options));
// If we had other per-element init, we'd do it here.
});
// Let's have our "nifty" set up an event handler when initialized.
// We do it here because we don't need a separate handler for each
// individual element.
this.on("click.nifty", function() {
// Resond by setting the color according to this specific
// element's options.
var $this = $(this);
$this.css("color", $this.data(optionsName).color);
});
// The main init of a plugin should almost always return `this`
// for chaining
return this;
}
// Plugins should always have a "destroy" method that undoes what they do
// in init. It should handle it gracefully if init hasn't been called on
// the elements.
function nifty$destroy() {
// Remove our event handler
this.off("click" + eventNS);
// Remove the event data
return this.each(function() {
$(this).removeData(optionsName);
});
}
// Our "options" method
function nifty$options(newOptions) {
// For each element in the matched set, update the options.
// Note how we're returning the return value from `this.each`, which is
// `this`. A method should always return `this` unless it has a good
// reason for doing something else.
return this.each(function() {
var $this = $(this);
var opts = $this.data(optionsName);
checkForInit("options", opts);
$this.data(optionsName, $.extend({}, opts, newOptions));
});
}
// Our "settext" method
function nifty$settext() {
// Set the text on each matched element individually, according to its
// options.
return this.each(function() {
var $this = $(this);
var opts = $this.data(optionsName);
checkForInit("settext", opts);
$this.text(opts.text);
});
}
// Worker function, throws exception if a method is called on an
// un-initialized element.
function checkForInit(method, opts) {
if (typeof opts === "undefined") {
throw new Error(method + " cannot be called until `nifty` has been initialized on the element");
}
}
})(jQuery);
// Using it -- init
$(".foo").nifty({color: "blue"});
$(".bar").nifty({text: "bar text"});
// Calling methods - give different texts to each of the foos, override
// the color we use on the last one
$(".foo")
.first()
.nifty("options", {text: "I'm the first foo"})
.end()
.last()
.nifty("options", {text: "I'm the last foo", color: "orange"});
// Call "settext" on all the foos and bars
$(".foo, .bar").nifty("settext");
// Destroy "nifty" on bar
$(".bar").nifty("destroy");
// Prove that we did and that methods complain
try {
$(".bar").nifty("options", {color: "blue"});
console.log("Shouldn't get here");
} catch (e) {
console.log("Correctly got error: " + e.message);
}
<div>Click each of the three divs below.</div>
<div style="padding: 8px; border: 1px solid #ddd">
<div class="foo">foo1</div>
<div class="foo">foo2</div>
<div class="bar">bar</div>
</div>
<div>Note that clicking the 'bar' element doesn't change its color, because we destroyed the nifty plugin on it after using nifty to set its text</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
実例がありますか?これは私がこれまで行ってきたことで、これは常に最後に定義されたオブジェクトを指しています。 –
@JosephKhella:これを追加しました。 :-) –
Crowder、私はあなたのコードを編集して、必要なものと一致させました。私のソースコードで何が間違っているのかまだ分かりませんが、あなたの例では動作しますが、 thx man :) 'http:// jsfiddle.net/Q8KVC/1497 /' –
- 1. パイソン - 私は関数を作成し、私は<code>**criterias</code>を呼び出すキーワードとして、パラメータのセットでそれを呼び出すことができるようにしたい関数の引数として(ステップなし)
- 2. どのように私はこの関数をhaskellで呼び出すことができますか?
- 3. PHPは、これは簡単かもしれませんが、PHP</p> <p>に初心者や学習であると私は私はそれように$リストと呼ばれる変数に出力を入れたいそれのための解決策を得ることができませんでし変数
- 4. は、どのように私はこの答えはここにあるが、私はそれを見つけることができなかった(または私はそれを見たときに、少なくとも<em>は</em>それを認識!)</strong>を知っ<strong>HTMLの画像のsrcタグ
- 5. <cr>を関数に再マップした場合、関数内の元の<cr>をどう呼び出すことができますか?
- 6. C++ - どのように私はどこでも静的関数を呼び出すことができます、そのプライベート変数を変更する?
- 7. cellForRowAtIndexPathがnumberOfRowsInSectionが、私は</p> <p>ServerDataControllerでこのViewControllerをを呼び出す と呼ばれているにもかかわらず、cellForRowAtIndexPathが呼び出されない理由を理解することはできません
- 8. getattrを使用した場合と同じファイルから関数を呼び出すにはどうすればよいですか?私は別のファイルからこれらの関数を呼び出すとシェルでそれらを行うことができます現時点では私のプログラムで
- 9. ウィンドウタブのアンギュアリスで関数(UNBOUND関数)を呼び出すにはどうすればいいですか?ウィンドウが閉じられるか場所が <p>私のサンプル関数の呼び出しがある</p>を変えた上で、私は、コントローラ上のAJAX呼び出しを持っている
- 10. 呼び出しMavenのゴール<p>それは私が同等の操作を行うことができ、例えば、JavaからMavenのゴールを呼び出すことは可能ですか?</p> <p>おかげで、 ニック</p>
- 11. 私はのようにjqueryの機能をしたようにどのように私は、私の角度の関数でjQueryの関数を呼び出すことができることを知ってほしい
- 12. それを呼び出した関数を呼び出す関数を取得するにはどうすればよいですか?
- 13. tabBarControllerのタブを切り替えると、どのメソッドが呼び出されますか?私はtabBarController</p>私はそれが私が知りたい、viewDidLoadメソッドを呼び出します初めてで知っ <p>でタブを切り替えるときにメソッドが呼び出されます
- 14. は、私は、コンソールにエラーを取得しています</em>)だから私はここが午前問題は</em>は(<em>機能HideLogin内部で呼び出しません<em>関数フレーム()ということである
- 15. 私はどのようにC++関数のsize_t *をObjective.cosで呼び出すことができますか?
- 16. このNSArrayをJSONSerializeできないのはなぜですか?次のように私はNSFetchRequestから取得するが、私はisValidJSONObjectを呼び出すとき、私はそれがコード有効</p> <p>ではない私に指示節に終わることにNSArrayをJSONSerializeしようとしています
- 17. 私はそれを公開するとしても、intを呼び出すことはできません
- 18. は、私はロックと呼ばれる世代のサーバーのグループを持っている場合は、私はそのgen_serverの個々のプロセスのPIDから</p> <p><code>hello() -> io:format("Hello, world!~n").</code></p> <p>を言う関数を呼び出すことができますそのはPid
- 19. は、私は私はそれをどのように行うことができます</p> <pre><code>wadl: http://domain.com/application.wadl method: checkInfo </code></pre> <p>WADLサービスからメソッドを呼び出し、パラメータを渡す必要がPHP
- 20. 呼び出されたバットファイルをどのようにして呼び出すことができますか?
- 21. どのように私はPHPからCプログラムの関数を呼び出すことができますか?
- 22. "$(obj).my_function()"のように私の関数をどのように呼び出すことができますか?
- 23. Chromeはどのように関数呼び出しスタックを検出しますか?私はpuppeteerで直接呼び出すことはできますか?
- 24. freemarker:私は呼び出し元のURLを知ることができる方法はありますか?
- 25. あなたは私が欠けていると思うどのような種類の機能のこれは..です__cdecl、__stdcall、または__thiscall</p> <p>求めることができる通常の関数</p> <p>.. C++でASM関数を呼び出すしようとするとC++
- 26. Redirect_toと私はこれを実行していた場合、私は</p> <p>は、レンダリングおよび/またはこのアクション内で複数回呼び出されたリダイレクトなどのエラーを取得しています
- 27. 私は私がきたビデオhtml要素を作成し、別のWebページから</p> <p>をソース要素を取得することにより、ソースを取得しますJavaScriptで関数を作成しようとしているJavaScriptの
- 28. 私はプライベートメソッドを呼び出すことができますか?
- 29. jQueryでは、要素が「マウスオーバーされた」とその要素の子要素を検出するにはどうすればよいですか?ここ
- 30. gdbの入れ子関数呼び出しをどのように進めることができますか?
あなたの懸念に加えて、私はこれは私が「ものではありませんのコースWaldirJ.PereiraJunior @ –
'' $( "#のmyDiv1")のようなあなたは、id'選択 'は'# 'を使うべきだと思います私は話しています。これは間違いなく私の問題ではありません –