jQuery After() 方法不适用于 id 属性?
下面的代码工作正常。但是如果我将 newPara() 中的 html 字符串更改为 或
它不起作用。这里面有什么问题呢?
我需要该 id 属性来删除新附加的 html。
任何建议!
谢谢!
<html>
<head>
<style>
p { background:yellow; font-weight:bold; cursor:pointer; padding:5px; }
p.over { background: #ccc; }
span { color:red; }
</style>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<div id="aDiv">
<p>Click me!</p>
</div>
<span></span>
<script>
$("body").delegate("p", "click", newPara);
$("body").undelegate("button", "click", newPara).find("#adiv").html("<p>Click me!</p>");
function newPara() {
var html = "<textarea></textarea><button>Save</button>";
$(this).after(html);
}
</script>
</body>
</html>
The below code is working fine. But if i change the html string in newPara() to <textarea></textarea><button id="someId">Save</button>
or <textarea></textarea><input type="button" value="Save"/>
its not working. What is the problem in this?
I need that id attribute to remove the newly attached html.
Any Suggestions!
Thanks!
<html>
<head>
<style>
p { background:yellow; font-weight:bold; cursor:pointer; padding:5px; }
p.over { background: #ccc; }
span { color:red; }
</style>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<div id="aDiv">
<p>Click me!</p>
</div>
<span></span>
<script>
$("body").delegate("p", "click", newPara);
$("body").undelegate("button", "click", newPara).find("#adiv").html("<p>Click me!</p>");
function newPara() {
var html = "<textarea></textarea><button>Save</button>";
$(this).after(html);
}
</script>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我唯一的猜测是您使用双引号来创建字符串文字,但也在字符串中使用它们,因此提前终止它。
如果是这样,这个:
应该是:
请注意,我在外面使用了单引号,这样里面的单引号就不会关闭字符串。
尝试一下: http://jsfiddle.net/QQrHr/
该示例使用你的确切代码。我所做的唯一更改是在字符串周围使用单引号。
如果您愿意,您也可以转义内部引号:
My only guess would be that you're using double quotes to create the string literal, but also using them inside the string, therefore terminating it early.
If so, this:
should be:
Notice that I used single quotes on the outside, so that the inside ones don't close the string.
Try it out: http://jsfiddle.net/QQrHr/
The example uses your exact code. The only change I made was to use singe quotes around the string.
You could also escape the inner quotes if you wanted:
您是否在寻找追加而不是之后?或者甚至 http://api.jquery.com/insertAfter/ ?
Are you looking for append instead of after? Or even http://api.jquery.com/insertAfter/ ?