Javascript ajax onchange后Jquery插件不工作
这是我的代码:(Jquery)Javascript ajax onchange后Jquery插件不工作,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,这是我的代码:(Jquery) var loadCSS=函数(url,回调){ var link=document.createElement('link'); link.type='text/css'; link.rel='stylesheet'; link.href=url; link.id=‘主题风格’; document.getElementsByTagName('head')[0].appendChild(链接); var img=document.createElement('im
var loadCSS=函数(url,回调){
var link=document.createElement('link');
link.type='text/css';
link.rel='stylesheet';
link.href=url;
link.id=‘主题风格’;
document.getElementsByTagName('head')[0].appendChild(链接);
var img=document.createElement('img');
img.onerror=函数(){
如果(回调)回调(链接);
}
img.src=url;
}
$(文档).ready(函数(){
var initEditor=函数(){
$(“.new_text”).sceditor({
插件:“bbcode”,
样式:“./minified/jquery.sceditor.default.min.css”
});
};
$(“#主题”).change(函数(){
var theme=“./minified/themes/”+$(this.val()+”.min.css”;
$(“.new_text”).sceditor(“实例”).destroy();
$(“link:first”).remove();
$(“#主题样式”).remove();
loadCSS(主题,initEditor);
});
initEditor();
});
这段代码在Ajax更改函数后不起作用。我的html代码如下:
以下是ajax响应:
<div id="txtHint" style="margin-left: 160px;">
<textarea class="new_text" name="about_builder" id="about_builder" style="height:200px;width:600px;">
<?php echo $row_ab['about_builder']; ?>
</textarea></div>
问题是在ajax之后,文本区插件无法工作。当您使用ajax添加新html时,需要始终调用
.sceditor
。您还应该修改该函数,以便仅对新添加的元素调用sceditor
var initEditor = function(context) { //Add a context parameter to search only from this context
$(".new_text",context).sceditor({
plugins: 'bbcode',
style: "./minified/jquery.sceditor.default.min.css"
});
};
您的ajax函数如下所示:
$.ajax({
//
})
.done(function(data){
yourContainer.html(data); //When you load the html, you need to insert it into the DOM
initEditor(yourContainer);//call the sceditor on the newly added element.
});
您所说的“这段代码在Ajax更改功能后不起作用”是什么意思?你预计会发生什么,会发生什么?是否有错误?您需要在ajax调用后再次绑定您的
sceditor
,或者使用委派来确保自动绑定新添加的元素。是的,sceditor在ajax之前工作。但在ajax之后,编辑器就不起作用了。只有文本区域显示我不了解您的方法@Khanh@user3115202:从ajax附加html时,需要为新html调用.sceditor
$.ajax({
//
})
.done(function(data){
yourContainer.html(data); //When you load the html, you need to insert it into the DOM
initEditor(yourContainer);//call the sceditor on the newly added element.
});