简体   繁体   English

jQuery附加文本是不可见的

[英]jQuery appended text is invisible

I'm trying to create a couple of buttons above a textarea to insert some HTML code -- a VERY poor-man's HTML editor. 我正在尝试在textarea上面创建几个按钮来插入一些HTML代码 - 一个非常糟糕的HTML编辑器。 I have a couple of INPUT elements, and I'm using jQuery to set a click handler that will call's jQuery's append() or html() or text() functions. 我有几个INPUT元素,我正在使用jQuery设置一个单击处理程序,它将调用jQuery的append()html()text()函数。

The handler fires, it shows a debug alert(), but the text I'm trying to append doesn't show up in the textarea. 处理程序触发,它显示调试alert(),但我试图追加的文本没有显示在textarea中。 When I inspect the textarea in Firebug, I see the text I'm appending as a child of the textarea -- but it's dimmed, as when an element's style is set to display:none. 当我检查Firebug中的textarea时,我看到我作为textarea的子项附加的文本 - 但它变暗了,就像元素的样式设置为display:none。 But Firebug's CSS inspector doesn't show any change to the display or visibility properties. 但Firebug的CSS检查员没有显示任何显示或可见性属性的变化。

When I set the click handler to 'append()', and then click multiple times, in Firebug I see the text being added over and over again -- but each new chunk is still invisible. 当我将click处理程序设置为'append()',然后单击多次时,在Firebug中我看到一遍又一遍地添加文本 - 但每个新块仍然是不可见的。 If I choose 'Edit HTML' in Firebug and then type some chars next to the appended text, the entire text block -- the text added by jQuery and the stuff I added in Firebug -- suddenly appear. 如果我在Firebug中选择“编辑HTML”,然后在附加文本旁边键入一些字符,整个文本块 - 由jQuery添加的文本和我在Firebug中添加的内容 - 突然出现。

This also happens if I don't use a click handler, but call my append function using an inline handler like onclick="javascript:insert('bold');" 如果我不使用单击处理程序,但使用内联处理程序(如onclick="javascript:insert('bold');"调用我的追加函数,也会发生这种情况onclick="javascript:insert('bold');"

Anyone have any idea why the appended text is not displayed? 任何人都知道为什么没有显示附加文本?

Here's the relevant code: 这是相关的代码:

The HTML: HTML:

<input type='button' id='bold' value='B' onclick='javascript:insert("bold")' />

<textarea name='PersonalGreeting' id='PersonalGreeting'>default text</textarea>

The Javascript: Javascript:

function insert( cmd )  {
    switch ( cmd )  {
        case 'bold':
            $('#PersonalGreeting').append('<b>bold text here</b>');
            break;  
    }
}

I would guess that jQuery is trying to append HTML DOM elements to the textarea . 我猜想jQuery正试图将HTML DOM元素附加到textarea

Try using the val method to get and set the textarea 's value, like this: 尝试使用val方法来获取和设置textarea的值,如下所示:

$('#PersonalGreeting').val($('#PersonalGreeting').val() + '<b>bold text here</b>');

The basic problem is that you can't put HTML inside a <textarea> . 基本问题是您不能将HTML放在<textarea> In fact, you can't append HTML elements to one at all. 实际上,您根本无法将HTML元素附加到一个元素。 You could use the .val() method to change the text shown inside, but that won't make it bold. 您可以使用.val()方法更改内部显示的文本,但不会使其变为粗体。 That will just make it have <b> showing as part of the text. 这只会使<b>显示为文本的一部分。

An off-the-shelf WYSIWYG editor like TinyMCE is free and easy to implement. TinyMCE这样的现成的WYSIWYG编辑器是免费且易于实现的。 Rather than reinvent the wheel (which is a lot harder than it might look), try an existing wheel out. 不要重新发明轮子(这比它看起来难得多),试试现有的轮子。

SLaks and VoteyDisciple are correct. SLaks和VoteyDisciple是正确的。 You're usage of append is faulty as you are perceiving it as a string function. 当你将它视为字符串函数时,你对append的使用是错误的。

From http://docs.jquery.com/Manipulation/append 来自http://docs.jquery.com/Manipulation/append

Append content to the inside of every matched element. 将内容附加到每个匹配元素的内部。 This operation is the best way to insert elements inside, at the end, of all matched elements. 此操作是在所有匹配元素的末尾插入元素的最佳方法。 It is similar to doing an appendChild to all the specified elements, adding them into the document. 它类似于对所有指定元素执行appendChild,将它们添加到文档中。

Reinventing the wheel on this one is likely more headache than its worth unless this is an attempt to create a superior, competing product or for your own experimentation. 除非这是为了创造一个优越的竞争产品或者为了您自己的实验,重新发明这一点可能比其价值更令人头痛。

Also, I would shy away from use of obtrusive JavaScript as you have shown in your example with onclick='javascript:insert("bold")' embedded in the input element. 此外,我会回避使用突出的JavaScript,因为您在示例中显示了onclick='javascript:insert("bold")'嵌入在input元素中。 Instead, you'll have a more elegant solution with something like the following: 相反,您将拥有一个更优雅的解决方案,如下所示:

HTML HTML

<input type="button" value="B" class="editor-command" >
<input type="button" value="I" class="editor-command" >
<input type="button" value="U" class="editor-command" >

JavaScript (not tested) JavaScript (未经测试)

$(document).ready(function() {
  var textarea = $('#PersonalGreeting')
  $(".editor-command").each(function(i, node) {
    textarea.val(textarea.val() + '<$>text here</$>'.replace(/\$/g, node.value);
  });
});

If the main issue is the textarea not being visible, I would try this: 如果主要问题是textarea不可见,我会尝试这样做:

$('#PersonalGreeting').append('<b>bold text here</b>').show();

Might be worth a shot. 可能值得一试。

edit: In the vain of not trying to reinvent the wheel, I've had success with WYMEditor 编辑:没有试图重新发明轮子是徒劳的,我在WYMEditor上取得了成功

You could do this: 你可以这样做:

 $('#PersonalGreeting').append('[b]bold text here[/b]');

But that won't actually render the text as bold. 但这实际上并不会使文本变为粗体。 To be honest I'm not actually sure how to render text as bold inside a textarea, I imainge some js trickery. 说实话,我实际上并不确定如何在textarea中将文本渲染为粗体,我还有一些js技巧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM