在JavaScript中包含jQuery
Include jQuery in JavaScript
我有两段代码想一起工作,但我无法将这个jQuery片段正确地集成到JavaScript中…
JavaScript:
(function(){
// Creates the plugin
tinymce.create('tinymce.plugins.mygallery', {
// Creates control instances based on the control's ID.
createControl : function(id, controlManager) {
if (id == 'mygallery_button') {
// Creates the button
var button = controlManager.createButton('mygallery_button', {
title : 'MyGallery Shortcode', // Title of the button
image : '../wp-includes/images/smilies/icon_mrgreen.gif', // Path to the button's image
onclick : function() {
// Triggers the thickbox
var width = jQuery(window).width(), H = jQuery(window).height(), W = ( 720 < width ) ? 720 : width;
W = W - 80;
H = H - 184;
tb_show( 'My Gallery Shortcode', '#TB_inline?width=' + W + '&height=' + H + '&inlineId=mygallery-form' );
}
});
return button;
}
return null;
}
});
// Registers the plugin
tinymce.PluginManager.add('mygallery', tinymce.plugins.mygallery);
// Executes this when the DOM is ready
jQuery(function(){
// Creates a form to be displayed everytime the button is clicked
var form = jQuery('<div id="mygallery-form"><table id="mygallery-table" class="form-table">'
<form id="myForm">'
<div id="input1" style="margin-bottom:4px;" class="clonedInput">'
Name: <input type="text" name="name1" id="name1" />'
</div>'
<div>'
<input type="button" id="btnAdd" value="add another name" />'
<input type="button" id="btnDel" value="remove name" />'
</div>'
</form>'
</div>');
[…]
});
})()
jQuery:
$(document).ready(function() {
$('#btnAdd').click(function() {
var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
var newNum = new Number(num + 1); // the numeric ID of the new input field being added
// create the new element via clone(), and manipulate it's ID using newNum value
var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);
// manipulate the name/id values of the input inside the new element
newElem.children(':first').attr('id', 'name' + newNum).attr('name', 'name' + newNum);
// insert the new element after the last "duplicatable" input field
$('#input' + num).after(newElem);
// enable the "remove" button
$('#btnDel').attr('disabled','');
// business rule: you can only add 5 names
if (newNum == 5)
$('#btnAdd').attr('disabled','disabled');
});
$('#btnDel').click(function() {
var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
$('#input' + num).remove(); // remove the last element
// enable the "add" button
$('#btnAdd').attr('disabled','');
// if only one element remains, disable the "remove" button
if (num-1 == 1)
$('#btnDel').attr('disabled','disabled');
});
$('#btnDel').attr('disabled','disabled');
});
仅供参考:JS用表单(WordPress)创建了一个弹出窗口,我想用jQuery片段实现用户可以动态添加更多输入字段的功能。
谢谢你的帮助!
您可以将jQuery片段放在javascript代码之后。这不应该是问题所在,因为jQuery代码只是绑定事件。
但是,我不知道#btAddd和#btndl在哪里。问题可能是,元素是在运行jQuery代码后创建的,因此当您调用以下代码时:$('#btnAdd').click(function(){...});
元素#btAddd不在那里。
试试这个:$(document).on('click', '#btnAdd', function(){...});
这将把点击事件绑定到文档(始终存在),而不是#btAddd。不过,为了提高性能,您应该将它绑定到#btAddd的父级,当文档准备好时,您确信该父级存在。
相关文章:
- jQuery包含html-将类添加到头中
- Javascript,jquery.panzoom,jquery:包含问题
- Richfaces捆绑的jQuery包含在webapp捆绑的JavaScript文件之后
- 如何在没有冲突的情况下将 JQuery 包含在 JavaScript 文件中
- 使用noConflict jQuery包含带有requireJS的jQuery UI
- 将jQuery包含到javascript中,并在imacros中使用它
- jQuery包含多个字符串优化
- jQuery :包含始终触发的选择器
- 更改 <对象> 标记的属性,该标记使用 jQuery 包含一个 SVG 元素
- 如何在使用浏览器时将jQuery包含在AngularJS中
- Jquery包含删除某些字符
- 如何使用 jquery 包含树枝模板
- 如何将Javascript和jQuery包含在HTML中
- Jquery包含字符串
- 使用jQuery包含文件并获取它's的内容长度
- jQuery包含来自输入文本
- 如何使用Jquery(包含页面)重新加载PHP查询
- jQuery:包含<选项>VALUE</选项>
- jQuery“包含”代码在Chrome上不起作用
- jQuery:包含在ie7中不工作的内容(jQuery 1.3.5)