Javascript 在jQuery插件中获取集合
基本上,我要做的是创建一个bbcode编辑器,其中包含一个文本框、一些按钮和jQuery。这是我的表格:Javascript 在jQuery插件中获取集合,javascript,jquery,Javascript,Jquery,基本上,我要做的是创建一个bbcode编辑器,其中包含一个文本框、一些按钮和jQuery。这是我的表格: <div class="form-group"> <div class="btn-group btn-group-sm"> <button type="button" class="btn glyphicon bbcode" rel="bold"><b>B</b></button>
<div class="form-group">
<div class="btn-group btn-group-sm">
<button type="button" class="btn glyphicon bbcode" rel="bold"><b>B</b></button>
<button type="button" class="btn glyphicon bbcode" rel="italic"><i>I</i></button>
</div>
</div>
<div class="form-group">
<textarea class="bbcode" rel="editor" cols="100" rows="12"></textarea>
</div>
这似乎是我获取文本框的唯一方法,我真的不想这样硬编码我想要的类。我想我要寻找的是一种从脚本标记中的函数调用获取集合的方法
这很可能是我忽略的愚蠢/明显的事情。立即函数中This的值指的是集合。但是,它被单击处理程序(指正在单击的元素)中的
this
隐藏,因此您无法访问它
创建一个变量来存储这个,它就是您的集合
(function ($) {
"use strict";
$.fn.bbcode = function () {
var $editors = this;
this.click(function () {
var rel = $(this).attr('rel');
if (rel == 'editor') {
return this;
} else {
alert($(this).attr('rel')); // I can see this pop up so the click event is firing
$editors.val('test');
return this;
}
});
}
}(jQuery));
你说的“集合”是什么意思?你想要
$(this).find(“[rel=editor]”)val(“test”)
?在这个例子中,它的集合是由$('.bbcode')返回的DOM元素,也许这是一个错误的术语?@soktinpk不起作用,我认为$(this)
是我实际单击的元素
(function($) {
"use strict";
$.fn.bbcode = function() {
this.click(function() {
var rel = $(this).attr('rel');
if (rel == 'editor') {
return this;
} else {
alert($(this).attr('rel')); // I can see this pop up so the click event is firing
$('.bbcode[rel=editor]').val('test');
return this;
}
});
}
} (jQuery));
(function ($) {
"use strict";
$.fn.bbcode = function () {
var $editors = this;
this.click(function () {
var rel = $(this).attr('rel');
if (rel == 'editor') {
return this;
} else {
alert($(this).attr('rel')); // I can see this pop up so the click event is firing
$editors.val('test');
return this;
}
});
}
}(jQuery));