开发者

JQuery change text to input

开发者 https://www.devze.com 2022-12-08 23:36 出处:网络
I have a table setup like below: <table> <tr> <td class=\"url\"> <a href=\"http://www.domainname.com/page1.html\" />

I have a table setup like below:

<table>
  <tr>
    <td class="url">
       <a href="http://www.domainname.com/page1.html" />
    </td>
  </tr>
  <tr>
    <td class="url">
       <a href="http://www.domainname.com/page2.html" />
    </td>
  </tr>
  <tr>
    <td class="url">
       <a href="http://www.domainname.com/page3.html" />
    </td>
  </tr>
</table>

I basically want the anchor to change to a text box containing the href when the link is click, below are example results:

<table>
  <tr>
    <td class="url">
       <input type="text" value="http://www.domainname.com/page1.html" />
    </td>
  </tr>
  <tr>
    <td cla开发者_JS百科ss="url">
       <a href="http://www.domainname.com/page2.html" />
    </td>
  </tr>
  <tr>
    <td class="url">
       <a href="http://www.domainname.com/page3.html" />
    </td>
  </tr>
</table>

When another anchor tag is click or the text box is unfocused, any text boxes go back to being a anchor and the clicked one changes to a text box.


This is a start. Add a click event to the links and a blur event to the inputs using live().

$(function() {
  $("td.url a").live("click", function() {
    var parent = $(this).parent();
    $(this).replaceWith("<input type='text' value='" + $(this).attr("href") + "'>"); //closing angle bracket added
    parent.children(":text").focus();
    return false;
  });
  $("td.url :text").live("blur", function() {
    $(this).replaceWith("<a href='" + $(this).val() + "'>");
  });
});

That being said, for this kind of thing I prefer not to delete elements from the DOM like this. Instead I prefer to just hide/show elements as appropriate. For example:

<table>
  <tr>
    <td class="url">
       <a href="http://www.domainname.com/page1.html" />
       <input type="text">
    </td>
  </tr>
</table>

with:

td.url input { display: none; }
td.edit a { display: none; }
td.edit input { display: block; }

and

$(function() {
  $("td.url a").click(function() {
    var a = $(this);
    a.next().val(a.attr("href")).focus();
    a.parent().addClass("edit");
    return false;
  });
  $("td.url :text").blur(function() {
    var txt = $(this);
    txt.prev().attr("href", txt.val());
    txt.parent().removeClass("edit");
  });
});


$(".url").click(function()
{
var link = $(this).find("a");
link.hide();
$(this).append("<input id'txtUrl' value" + link.attr("href") + " />");
});


Something like:

$("td.url a").bind("click", function(){
    var clickevent = arguments.callee;
    $("td.url input").trigger("blur");
    $(this).replaceWith("<input type='text' value='"+$(this).attr("href")+"'/>");
    $("td.url input").bind("blur", function(){
        $(this).replaceWith("<a href='"+$(this).val()+"' />");
        $("td.url a").bind("click", clickevent);
    });
});

totally untested, of course

0

精彩评论

暂无评论...
验证码 换一张
取 消