[英]Replace a title with an input and update the title on the database
嗨,我正在尝试通过文本输入单击标题以修改标题,然后将所做的修改提交到数据库,我正在使用以下代码:
<div style="font-size: 70%;"><h2 class="outer"><?php echo $designation; ?> </h2></div>
该div使用其他脚本加载,因此不在原始页面上,因此我认为我们必须使用委托方法。
这是我用来将其背景颜色变为粉红色的jQuery脚本:
<script src="jquery-1.10.2.min.js">
$(document).ready(function(){
$("#right").delegate("h2","click",function(){
$("h2").css("background-color","pink");
});
});
</script>
知道如何用文本输入标签替换此div中的标题吗? 当我在输入字段之外单击时,如何将修改提交到数据库? 谢谢
.on()
方法 input
元素没有结束标记,您应该删除</input>
<script src="jquery-1.10.2.min.js"></script> <script> $(document).ready(function(){ // When the DOM is ready $(".outer").on('click', function() { if ( this.children.length ) return; var text = $.trim(this.innerHTML); $("<input>").val(text) .appendTo($(this).empty()) .focus(); }).on('blur', 'input', function() { // Listening to blur event // for replacing the element with it's value $(this).replaceWith( $.trim(this.value) ); }) }) </script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.