jQuery get text 包括单引号 using text() 无法正确显示
jQuery get text include single quote using text() doesn't show properly
所以我想在单击按钮时将div的文本显示为输入值。但是,如果文本包含单引号,则无法正确显示。
<div id="content_comment">It's Yours!</div>
<button id="btn_comment_edit">Edit</button>
// jQuery
$('.btn_comment_edit').click(function(){
$('#content_comment').replaceWith("<form method='post'><input type=text value='"+$('#content_comment').text()+"'></form>")
});
// result
// It' (doesn't show after single quotes)
它应该It's Yours!
输入值中。如何用单引号显示整个语句?
你可以做这样的事情
$('#content_comment').replaceWith("<form method='post'><input type=text value='''' +$('#content_comment').text()+'''></form>")
问题在于单引号和双引号的用法。您也在使用.btn_comment_edit
而不是#btn_comment_edit
。尝试如下。
$('#btn_comment_edit').click(function () {
$('#content_comment').replaceWith('<form method=post><input type=text value="' + $('#content_comment').text() + '"></form>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="content_comment">It's Yours!</div>
<button id="btn_comment_edit">Edit</button>
这就是编码的方式。
$('.btn_comment_edit').click(function(){
var a = $('#content_comment').text();
a = addslash(a);
$('#content_comment').replaceWith("<form method='post'><input type=text value='"+a+"'></form>")
});
function addslash(iid){
str=iid.replace(/"/g,''"');
str=iid.replace(/'/g,'''');
return str;
}
相关文章:
- text()显示双文本
- text()将保留旧值,但在DOM上显示新值
- OpenLayers.Layer.Text在0,0处显示图标
- 访问要在 React Native Text 组件中显示的对象数组
- D3.js条形图:不显示 .text
- 如何使用 JavaScript 显示 html 输入:.html(' < input type=“text” /
- ul li $(this).text() 什么也没显示
- 将text.innerHTML修改为显示/隐藏脚本中的图像
- 当选择框在
中时,我如何显示它,并且我在 javascript 中执行 p.text(“test”)
- jQuery get text 包括单引号 using text() 无法正确显示
- 在 KineticJs Text() 上显示值
- 什么类型的对象在控制台中显示为[object Text]
- AngularJS-动态创建<text区域>使用ngSanitize不会't显示(其他元素显示)
- 如何在jQuery的.text()中显示新行
- TypeError:无法读取属性'text显示元素值'的未定义
- 只要将字段中的Javascript值添加到另一个字符串中,就会显示为[object Text]
- 用于在网页上显示源自<text区域>
- 在innerHTML中添加p元素后,输入[type="text"]的值不显示
- Sublime Text显示CSS &一行Javascript
- 使用text()显示javascript代码