gpt4 book ai didi

jquery - 从下拉列表中插入文本到所见即所得

转载 作者:太空宇宙 更新时间:2023-11-04 15:27:07 24 4
gpt4 key购买 nike

有没有办法使用 jquery 将文本插入 Bootstrap 所见即所得?顺便说一句,我从这个页面使用了 bootstrap-wysihtml5 http://jhollingworth.github.io/bootstrap-wysihtml5/文本实际上是下拉列表中的模板,然后应将所选的 onchange 值插入到 wysisyg 中。

我试过这段代码,但它只在隐藏文本区域而不是所见即所得中插入文本

$(document).ready(function(e) {
$("#template").change(function(){
var template = $("#template").val();
if(template != '') {
$('.textarea, .wysihtml5-sandbox').append(template);
}
});
});

最佳答案

您不能简单地从包含页面的 DOM 访问 textarea,因为实际的 WYSIWYG 编辑器位于带有 .wysihtml5-sandbox 的 iFrame 中。所以要访问 iFrame 的 DOM 并修改它,我们必须首先访问 iFrame DOM:

  $('.wysihtml5-sandbox').contents() 
//Now we have the iFrame DOM to apply selectors to

$('.textarea', $('.wysihtml5-sandbox').contents())
//Now we have access to the textarea which contains the editor's current text

$('.textarea', $('.wysihtml5-sandbox').contents()).append('Hello World!')
//Now we have appended content to the editor view, the original goal

额外的东西:

$('.textarea', $('.wysihtml5-sandbox').contents()).removeClass('placeholder')
//Stops the text from being formatted as a placeholder (i.e. grayed out)
$('.textarea', $('.wysihtml5-sandbox').contents()).empty()
//Remove the existing placeholder text by emptying the textarea content

关于jquery - 从下拉列表中插入文本到所见即所得,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18635957/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com