如何在 TinyMCE 5 中动态更改占位符文本?

发布于 2025-01-09 05:18:40 字数 1676 浏览 0 评论 0原文

我正在使用 TinyMCE 5.6.2。我有一个与 TinyMCE 绑定的文本区域。占位符文本需要根据下拉列表的选择进行更改。我知道我可以用静态值初始化它:

 tinymce.init({
   selector: '.tinymce',
   placeholder: "Please enter the complete date.",

但是我需要根据下拉列表的选择来更改它(以下内容基于我尝试将文本区域转换为 TinyMCE 之前所做的事情)。

 if (reportingStatusId == "1") {
    document.getElementById('notes').placeholder = 'Please enter why not started and estimated start month.';
 } else if (reportingStatusId == "2") {
    document.getElementById('notes').placeholder = 'Please enter why not applicable.';
 } else if (reportingStatusId == "3") {
    document.getElementById('notes').placeholder = 'Please enter why not reported.';
 }

我已经尝试过以下方法,但都不起作用:

 if (reportingStatusId == "1") {
    tinyMCE.get('notes').placeholder = 'Please enter why not started and estimated start month.';
 } else if (reportingStatusId == "2") {
    tinyMCE.get('notes').placeholder = 'Please enter why not applicable';
 } else if (reportingStatusId == "3") {
    tinyMCE.get('notes').placeholder = 'Please enter why not reported';
 }

这是:

 if (reportingStatusId == "1") {
    tinymce.init({
       selector: ".tinymce",
       placeholder: "Please enter why not started and estimated start month.",
    });
 } else if (reportingStatusId == "2") {
    etc.....
 } else if (reportingStatusId == "3") {
    etc....
 }

这是我要定位的文本区域:

 <textarea id="notes" class="form-control statusfield tinymce" rows="3" style="background-color:white" asp-for="Model[0].notes" >@Model.Model[0].notes</textarea>

有没有办法动态更改 TinyMCE 文本区域的占位符文本?如果是这样,怎么办?

I'm using TinyMCE 5.6.2. I have a single textarea tied to TinyMCE. The placeholder text needs to change dependent upon the selection of a dropdown. I know I can initialize it with a static value:

 tinymce.init({
   selector: '.tinymce',
   placeholder: "Please enter the complete date.",

However I need to change it based on the selection of a dropdown (the following was based on what I was doing before I tried to convert the textarea to TinyMCE).

 if (reportingStatusId == "1") {
    document.getElementById('notes').placeholder = 'Please enter why not started and estimated start month.';
 } else if (reportingStatusId == "2") {
    document.getElementById('notes').placeholder = 'Please enter why not applicable.';
 } else if (reportingStatusId == "3") {
    document.getElementById('notes').placeholder = 'Please enter why not reported.';
 }

I've tried the following but neither works:

 if (reportingStatusId == "1") {
    tinyMCE.get('notes').placeholder = 'Please enter why not started and estimated start month.';
 } else if (reportingStatusId == "2") {
    tinyMCE.get('notes').placeholder = 'Please enter why not applicable';
 } else if (reportingStatusId == "3") {
    tinyMCE.get('notes').placeholder = 'Please enter why not reported';
 }

and this:

 if (reportingStatusId == "1") {
    tinymce.init({
       selector: ".tinymce",
       placeholder: "Please enter why not started and estimated start month.",
    });
 } else if (reportingStatusId == "2") {
    etc.....
 } else if (reportingStatusId == "3") {
    etc....
 }

Here is the textarea I am targeting:

 <textarea id="notes" class="form-control statusfield tinymce" rows="3" style="background-color:white" asp-for="Model[0].notes" >@Model.Model[0].notes</textarea>

Is there a way to change the placeholder text of a TinyMCE textarea dynamically? If so, how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

貪欢 2025-01-16 05:18:40

TinyMCE 并不正式支持在加载编辑器后修改占位符文本的功能。

如果您检查 TinyMCE 出现的 DOM,您将看到 TinyMCE 内的 标记上有一个名为 data- 的属性。 mce-占位符。您可以使用 JavaScript 修改该属性,这将导致占位符文本更新。这是一个运行示例:

https://fiddle.tiny.cloud/6Xhaab

关键行是:

tinymce.activeEditor.getBody().setAttribute('data-mce-placeholder', 'This is NEW placeholder text');

请注意,这依赖于 TinyMCE 将自身注入页面的当前方式以及它处理占位符的方式。如果 TinyMCE 将来要改变这一点,这可能会破坏这个解决方法,但对于 TinyMCE 5.x 来说,这应该适用于所有版本。

TinyMCE does not officially support the ability to modify the placeholder text once the editor is loaded.

If you inspect the DOM where TinyMCE appears you will see that there is an attribute on the <body> tag within the TinyMCE <iframe> called data-mce-placeholder. You can modify that attribute using JavaScript and that will cause the placeholder text to update. Here is a running example:

https://fiddle.tiny.cloud/6Xhaab

The key line is:

tinymce.activeEditor.getBody().setAttribute('data-mce-placeholder', 'This is NEW placeholder text');

Please note that this relies on the current way TinyMCE injects itself into the page and how it handles the placeholder. If TinyMCE were to change that in the future this would potentially break this workaround but for TinyMCE 5.x this should work across all versions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文