日期:2014-05-16 浏览次数:20475 次
<td class="noborder" colspan="2"> <div class="smallpicdiv" style="margin-left: 20px;"> <a href="../Item/ItemDetail?numIid=@item.NumIid" title="查看宝贝详情" target="_blank"> <img src="@item.PicUrl" class="smallpic" alt="点击查看宝贝详情" /> </a> </div> @{ var titleId = "item_" + @item.NumIid; } <div style="float: left;"> <p id="@titleId" class="itemTitleBg" onmouseover="showBgColor(this)" onmouseout="hiddenBgColor(this)" title="编辑" onclick="hiddenEle('@item.NumIid');" >@item.Title</p> </div> </td>
// 获得初始文本,设置为隐藏,然后判断是否已有输入域,如果没有则创建输入框 function hiddenEle(id) { var titleP = document.getElementById("item_" + id); titleP.style.display = "none"; if (!document.getElementById("title_input_" + id)) { buildEditDiv(titleP.parentNode, id); } } //创建输入框和保存按钮 function buildEditDiv(titleDiv, id) { var editText = document.createElement("input"); var titleP = document.getElementById("item_" + id); var oldTitle = titleP.firstChild.data; editText.setAttribute("class", "newItemTitle"); editText.setAttribute("type", "text"); editText.value = oldTitle; editText.name = id; editText.id = "title_input_" + id; titleDiv.appendChild(editText); titleDiv.appendChild(document.createElement("br")); var submitButton = document.createElement("button"); submitButton.appendChild(document.createTextNode("保存")); submitButton.id = "title_button_" + id; submitButton.onclick = function () { ajaxChangeTitle(id, oldTitle, editText.value); return false; } //这里输入框失去焦点后,设置了等待时间200ms才执行删除input和button操作,主要是为了在删除input之前,在button的onclick动作中能获得input的新值。 editText.onblur = function () { setTimeout(function () { titleDiv.removeChild(submitButton); titleDiv.removeChild(titleDiv.lastChild); titleDiv.removeChild(editText); titleP.style.display = ""; }, 200); } titleDiv.appendChild(submitButton); editText.focus(); } function ajaxChangeTitle(id, oldTitle, newTitle) { var titleP = document.getElementById("item_" + id); titleP.firstChild.data = newTitle; $.ajax( { url: '/TopDemo/Item/UpdateTitle', data: '{"iid":"' + id + '", "newTitle":"' + newTitle + '" }', type: "POST", contentType: "application/json;charset=utf-8", dataType: "json", success: function (data) { if (data == null || data.length == 0 || data == "false") { updateTitleError(id, oldTitle); } }, error: function (data) { updateTitleError(id, oldTitle) } }); } function updateTitleError(id, oldTitle) { var titleP = document.getElementById("item_" + id); titleP.firstChild.data = oldTitle; alert("更新宝贝标题失败"); } function showBgColor(ele) { ele.setAttribute("class", "itemTitleBg_c"); } function hiddenBgColor(ele) { ele.setAttribute("class", "itemTitleBg"); }