django summernote小部件元素的自定义javascript不起作用?
我在申请方面有一些问题 在小部件的工具栏中,我有一个按钮(django summernote小部件元素的自定义javascript不起作用?,javascript,jquery,django,summernote,Javascript,Jquery,Django,Summernote,我在申请方面有一些问题 在小部件的工具栏中,我有一个按钮(.btn全屏)。我想在用户单击此按钮时更改一些块,所以我添加了javascript,但不幸的是它不起作用 $(".note-toolbar").on("click", ".btn-fullscreen", function () { // Some code console.log('CLICK'); <!-- Dont work }); $(".btn-fullscreen").click(function(){
.btn全屏
)。我想在用户单击此按钮时更改一些块,所以我添加了javascript,但不幸的是它不起作用
$(".note-toolbar").on("click", ".btn-fullscreen", function () {
// Some code
console.log('CLICK'); <!-- Dont work
});
$(".btn-fullscreen").click(function(){
// Some code
console.log('CLICK'); <!-- Dont work
}
$(“.note toolbar”)。在(“单击“,”.btn全屏”,函数(){
//一些代码
log('CLICK');我找到了两种方法来归档它
首先是在根模板文件夹中新建的django\u summernote/widget\u inplace.html
,将代码复制到site packages/django\u summernote/templates/django\u summernote/widget\u inplace.html
:
{% load staticfiles %}
<div id='{{ id_src }}'>{{ value|safe }}</div>
<script>
$(function() {
var {{ id }}_textarea = window.document.getElementById('{{ id_src }}-textarea');
var {{ id }}_src = window.document.getElementById('{{ id_src }}');
var {{ id }}_settings = {{ settings|safe }};
var csrftoken = getCookie('{{ CSRF_COOKIE_NAME }}');
// include summernote language pack, synchronously
if( {{ id }}_settings.lang != 'en-US' ) {
$.ajaxSetup({async:false});
$.getScript('{{ STATIC_URL }}django_summernote/lang/summernote-' + {{ id }}_settings.lang + '.min.js');
$.ajaxSetup({async:true});
}
$({{ id }}_textarea).hide();
$summernote = $({{ id }}_src);
$summernote.summernote($.extend({{ id }}_settings, {
callbacks: {
onInit: function() {
var nEditor = $('.note-editor');
var nToolbar = $('.note-toolbar');
var nEditable = $('.note-editable');
var nStatusbar = $('.note-statusbar');
var setHeight = parseInt({{ id }}_settings.height) // default
- nToolbar.outerHeight() // toolbar height including margin,border,padding
- (nEditable.innerHeight() - nEditable.height()) // editable's padding
- (nEditor.outerHeight() - nEditor.innerHeight()) // editor's border
- nStatusbar.outerHeight(); // status bar height
nEditable.height(setHeight);
},
onBlur: function() {
{{ id }}_textarea.value = $(this).summernote('code');
},
{% if not disable_upload %}
onImageUpload: function(files) {
var imageInput = $('.note-image-input');
var sn = $(this);
// custom attachment data
var attachmentData = {{ id }}_textarea.dataset;
imageInput.fileupload();
var jqXHR = imageInput.fileupload('send',
{
files: files,
formData: $.extend({csrfmiddlewaretoken: csrftoken}, attachmentData),
url: {{ id }}_settings.url.upload_attachment,
})
.success(function (result, textStatus, jqXHR) {
data = $.parseJSON(result);
$.each(data.files, function (index, file) {
sn.summernote("insertImage", file.url);
});
})
.error(function (jqXHR, textStatus, errorThrown) {
// if the error message from the server has any text in it, show it
var msg = jqXHR.responseText;
if (msg.length > 0) {
alert('Got an error uploading an image: ' + msg);
}
// otherwise, show something generic
else {
alert('Got an error while uploading images.');
}
});
}
{% endif %}
}
}));
//-------YOUR CODE IN HERE------------------
// See https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
});
</script>
summernote代码是在自定义js之后呈现的,因此第二种方法是,如果您将js延迟5秒,那么您的自定义将在summernote代码初始化后运行,如下所示:
setTimeout(function () {
$(".btn-fullscreen").click(function () {
console.log("GG");
});
}, 5000);
这也是可行的,是推荐的。5秒可以改变1或2,你需要自己测试一个最佳延迟
{% load staticfiles %}
<div id='{{ id_src }}'>{{ value|safe }}</div>
<script>
$(function() {
var {{ id }}_textarea = window.document.getElementById('{{ id_src }}-textarea');
var {{ id }}_src = window.document.getElementById('{{ id_src }}');
var {{ id }}_settings = {{ settings|safe }};
var csrftoken = getCookie('{{ CSRF_COOKIE_NAME }}');
// include summernote language pack, synchronously
if( {{ id }}_settings.lang != 'en-US' ) {
$.ajaxSetup({async:false});
$.getScript('{{ STATIC_URL }}django_summernote/lang/summernote-' + {{ id }}_settings.lang + '.min.js');
$.ajaxSetup({async:true});
}
$({{ id }}_textarea).hide();
$summernote = $({{ id }}_src);
$summernote.summernote($.extend({{ id }}_settings, {
callbacks: {
onInit: function() {
var nEditor = $('.note-editor');
var nToolbar = $('.note-toolbar');
var nEditable = $('.note-editable');
var nStatusbar = $('.note-statusbar');
var setHeight = parseInt({{ id }}_settings.height) // default
- nToolbar.outerHeight() // toolbar height including margin,border,padding
- (nEditable.innerHeight() - nEditable.height()) // editable's padding
- (nEditor.outerHeight() - nEditor.innerHeight()) // editor's border
- nStatusbar.outerHeight(); // status bar height
nEditable.height(setHeight);
},
onBlur: function() {
{{ id }}_textarea.value = $(this).summernote('code');
},
{% if not disable_upload %}
onImageUpload: function(files) {
var imageInput = $('.note-image-input');
var sn = $(this);
// custom attachment data
var attachmentData = {{ id }}_textarea.dataset;
imageInput.fileupload();
var jqXHR = imageInput.fileupload('send',
{
files: files,
formData: $.extend({csrfmiddlewaretoken: csrftoken}, attachmentData),
url: {{ id }}_settings.url.upload_attachment,
})
.success(function (result, textStatus, jqXHR) {
data = $.parseJSON(result);
$.each(data.files, function (index, file) {
sn.summernote("insertImage", file.url);
});
})
.error(function (jqXHR, textStatus, errorThrown) {
// if the error message from the server has any text in it, show it
var msg = jqXHR.responseText;
if (msg.length > 0) {
alert('Got an error uploading an image: ' + msg);
}
// otherwise, show something generic
else {
alert('Got an error while uploading images.');
}
});
}
{% endif %}
}
}));
//-------YOUR CODE IN HERE------------------
// See https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
});
</script>
onBlur: function() {
{{ id }}_textarea.value = $(this).summernote('code');
},
setTimeout(function () {
$(".btn-fullscreen").click(function () {
console.log("GG");
});
}, 5000);