如何在使用jQuery时为占位符设置onblur和onfocus字符串
How to set onblur and onfocus string for placeholder when using jQuery
我已经尝试动态更新我的html,到目前为止,它正在将文本区域添加到页面中,但我定义的onblur和onfocus行为并不是因为在onblr和onfocu属性上包含了双引号。我的代码如下:
$('#answerList').append('<li><div class="row"><div class="eight columns"><textarea id="answerswerText" name="answerswerText" 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>
快乐编码:)
如果我理解您的问题,这个字符串可能会对您有所帮助。
'<textarea id="questionText" name="questionText" placeholder="Your question here..." onfocus="this.placeholder = ''''" onblur="this.placeholder = Your question here..."></textarea>'
Fiddlehttp://jsfiddle.net/aD38E/
包含连续双引号是个问题,这是正确的。简单的修复方法是用反斜杠转义双引号,如下所示:
onfocus="this.placeholder = '"'""
或
onblur="this.placeholder = '"Your answer here...'""
这是一个正在工作的JSFiddle:http://jsfiddle.net/ud36Y/
或者您可以使用以下代码:
$('input[type="text"]').focus(function () {
$(this).data('placeholder', $(this).attr('placeholder'))
.attr('placeholder', '');
}).blur(function () {
$(this).attr('placeholder', $(this).data('placeholder'));
});
// $('textarea') or $('input[type="email"]')
相关文章:
- 如何在ie7或更高版本中设置密码字段的占位符
- 在不使用javascript的情况下,可以在表单字段中设置文本占位符(以提示最终用户插入特定格式)
- 设置占位符值
- 如何设置元素的占位符文本 我无法编辑 HTML 并且 ID 正在更改
- 如果使用 jquery 值为空,则无法为占位符设置值属性
- 为 Internet Explorer 设置占位符
- 使用 Jquery 将占位符设置为文本框类型密码
- 我想使用 jQuery 在 html 中使用 data-* 属性将占位符设置为文本框
- 如何在使用jQuery时为占位符设置onblur和onfocus字符串
- 如何在加载视图时将日期设置为占位符
- 为在角度下拉列表中创建的默认空白选项设置占位符文本
- Select2 -当回调没有值时如何设置占位符
- 当变量设置时,在FORM中加载占位符标记
- 使用data-*来设置内部子节点的占位符
- 如何将占位符设置为选择框
- 使用javascript设置表单字段占位符属性
- 在内容占位符内设置href
- 使用angular-translate设置占位符值为onblur
- 我如何找到默认占位符设置
- 如何将占位符设置为表筛选器