【发布时间】:2022-01-07 13:12:01
【问题描述】:
我有这个 JavaScript
<script>
$(document).ready(function() {
$(".summernote").each(function() {
const self = $(this); // store self as this and this is referenced to each .summernote element.
self.summernote({
placeholder: "start typing",
lang: "en-GB",
height: 300, // set editor height
minHeight: null, // set minimum height of editor
maxHeight: null, // set maximum height of editor
focus: true, // set focus to editable area after initializing summernote
callbacks: {
onImageUpload: function(files) {
for (var i = 0; i < files.length; i++) {
send(files[i], self); // pass selft to send function
}
},
},
});
});
});
function send(file, context) {
var xhttp;
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
response = JSON.parse(this.responseText);
const urls = response.url;
urls.forEach(item => {
context.summernote('insertImage', item, function($image) {
$image.attr('src', item).attr('width', '100%');
});
});
}
};
var data = new FormData();
data.append("files[]", file);
xhttp.open("POST", "/admin/ans-img-upload", true);
xhttp.send(data);
}
</script>
我有这个 html
<textarea class="form-control summernote" id="summernote" name="af_options[]"> </textarea>
这适用于 javascript。
现在我正在尝试追加更多文本区域
function add_more_additional_field() {
$('#additional_options').append(
'<textarea class="summernote" id="summernote" name="af_options[]" placeholder="Start typing the answers here"></textarea>'
);
$(document).ready(function() {
$([...document.querySelectorAll('#additional_options .summernote')].pop()).summernote();
});
}
但不是所有的功能都像
context.summernote('insertImage', item, function($image) {
$image.attr('src', item).attr('width', '100%');
被附加了。
我需要帮助,了解如何在 javascript 中添加更多具有确切功能的 textarea 以像 $(document).ready() 中的那样工作......这是在网站实际加载时加载的那个
【问题讨论】:
-
当你有 jQuery 时为什么要
XMLHttpRequest? -
我不清楚你的意思。你在哪里调用你的
add_more_additional_field函数?您的标记中没有#additional_options元素,那么您希望该函数究竟能做什么?当您说“并非所有功能都已附加”时,您是什么意思? -
在
$(document).ready(function() {中所做的一切都在页面加载完成时执行一次,因此$(".summernote").each(function() {不包括事后添加的元素。 -
@LucaKiebel 事后如何重新格式化代码以包含元素?
-
添加新元素时再次运行
$(".summernote").each(function() {内的函数
标签: javascript function loops foreach summernote