开发者

How to transform text to input? [closed]

开发者 https://www.devze.com 2023-03-17 08:09 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

How can I create something such as:

- there is some text

- I click on it

- It tra开发者_开发问答nsforms into an input that contains the text I clicked on.


How about something like this

HTML

<div onclick="transform(this)">Some text here</div>

Javascript

function transform(obj)
{
    obj.innerHTML = "<input type='text' value='" + obj.innerHTML + "' />";
}

Hope I've understood your question correctly.


<a href="#" onclick="var input = document.createElement('input'); input.setAttribute('value', this.firstChild.nodeValue); this.parentNode.replaceChild(input, this);">here is some text</a>. Click it.

Instead of <a> you could also use <span> or <div> whatever you feel like.


Assuming HTML:

<p id='changeMe'>This is some text</p>

You can use jQuery:

$('#changeMe').click(function(){
    $this = $(this)
    $this.replaceWith( $('<input />').val( $this.text() ) )
})

See demo here: http://jsfiddle.net/aXZ8e/


<span id="target" onclick="javascript:run();" >This is dummy text</span>

<script type="text/javascript">
function run() {
  var span = document.getElementById( "target" );
  var text = span.innerHTML;
  var input = document.createElement("input");

  input.type = "text";
  input.value = text;

  span.innerHTML = "";
  span.appendChild( input );

  span.onclick = null;
}
</script>
0

精彩评论

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

关注公众号