javascript - 使用 jQuery 时如何为占位符设置 onblur 和 onfocus 字符串

标签 javascript jquery string append

我尝试动态更新我的 html,到目前为止它正在工作,文本区域被添加到页面中,但我定义的 onblur 和 onfocus 行为并不是由于我认为在onblur 和 onfocus 属性。我的代码如下:

$('#answerList').append('<li><div class="row"><div class="eight columns"><textarea id="answerText" name="answerText" placeholder="Your answer here..."  onfocus="this.placeholder = """ onblur="this.placeholder = "Your answer here...""></textarea></div><div class="four columns"><input type="checkbox" name="correctCheckbox" id="correctAnswerCheckbox' + answerPosition + '" value="' + answerPosition + '"/><label for="correctAnswerCheckbox' + answerPosition + '">Correct Answer</label></div></div></li>');

当我正常编码 HTML 时,我使用以下代码:

<textarea id="questionText" name="questionText" placeholder="Your question here..." onfocus="this.placeholder = ''" onblur="this.placeholder = 'Your question here...'"></textarea>

如何修复 JavaScript 以便这些属性发挥作用

最佳答案

“”(双引号)无法转义''(单引号)。但单引号可以转义双引号。

在你的情况下,我猜你是否仍然需要转义双引号内指定的单引号,然后使用反斜杠()。

<textarea id="questionText" name="questionText" placeholder="Your question here..." onfocus="this.placeholder = \'\'" onblur="this.placeholder =\ 'Your question here...\'"></textarea>

快乐编码:)

关于javascript - 使用 jQuery 时如何为占位符设置 onblur 和 onfocus 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18534197/

相关文章:

jquery - 如何在同一页面上创建 2 个 btn-group?

javascript - 星级评分插件问题

java - 计算字符串的小写字母

javascript - 使用本地 Pretty.css 在 javascript 中使用代码美化?

javascript - 如何连接两个 jQuery 元素对象? .add() 不起作用

javascript - 在 JavaScript 中进行 REST 调用而不使用 JSON?

c - 使用 malloc 时连接字符串长度的差异

c# - 如何以编程方式将 CamelCase 名称更改为可显示的名称

javascript - Knockout - 如何获取按钮的文本值?

javascript - 如何让部分 JS 代码不被压缩器压缩