問題の解決策が見つからなかったので、ここに投稿します。私は持っている
<td class="something"><i>some text</i></td>
そして、この「いくつかのテキスト」をクリックして入力タイプのテキストに変えたいと思います。これどうやってするの?
問題の解決策が見つからなかったので、ここに投稿します。私は持っている
<td class="something"><i>some text</i></td>
そして、この「いくつかのテキスト」をクリックして入力タイプのテキストに変えたいと思います。これどうやってするの?
.html()
クリックして置き換えるだけで使用できます$(this)
。これを試してみてください。
$(".something").click(function(){
$(this).html("<input type='text'/>");
});
デモはこちら
シンプルな JavaScript、JQuery なし
<i onclick="javascript:f(this);">some text</i>
<script type="text/javascript" language="javascript">
function f(i) {
i.innerHTML = "<input type=\"text\" value=\"" + i.innerHTML + "\" \/>";
}
</script>
$('.something').on('click', function(e) { // Rename node
var $this = $(this);
var length = $this.children().text().length;
var input = $('<input />', { 'type': 'text', 'value': $this.children().text(), 'data-old': $this.text() });
$this.parent().append(input);
$this.remove();
input.attr('size', length).focus();
}).on('blur', function(e) { // Save on blur
var $this = $(this);
var value = $this.val();
var oldValue = $this.attr('data-old');
$.post('YOUR URL', function (data) { // save the input data
if (data.success) {
$this.parent().append('<td class="something"><i contenteditable>' + value + '</i></td>'); // save successful
} else {
$this.parent().append('<td class="something"><i contenteditable>' + oldValue + '</i></td>'); // save error, use old value
}
$this.remove();
});
}).on('keyup', function(e) {
if (e.keyCode == 13) { // Enter key - trigger blur (save)
$(this)[0].blur();
} else if (e.keyCode == 27) { // Escape key - cancel, reset value
var $this = $(this);
$this.parent().append('<td class="something"><i contenteditable>' + $this.attr('data-old') + '</i></td>');
$this.remove();
}
});
解決策は次のとおりです。
$('.something').click(function() {
$(this).html('<input type="text" name="myname" />');
});
あなたが試すことができます
$('td.something').on('click',function(){
$(this).html('<input type="text" name="name" />');
});
入力テキストで td の値を取得する場合は、次のようにします。
$('td.something').on('click',function(){
$this = $(this);
$this.html('<input type="text" name="name" value="'+$this.text()+'" />');
});
ここにフィドルがあります:http://jsfiddle.net/vDktG/
$('.something i').click(function() {
$(this).replaceWith('<input type="text" value="' + $(this).text() + '">');
});
$('td.something i').on('click', function() {//bind event to <i> tag
this.prevHTML = $(this).html(); //can roll back if user cancels.
this.innerHTML = '<input>'; //input with no type specified defaults to text input
});
これを試して
<td class="something"><i onclick="turnIntoText(this)">some text</i></td>
<script type="text/javascript">
function turnIntoText(args) {
$(args).replaceWith('<input type="text" value="'+$(args).html()+'" />');
}
// or
// function turnIntoText(args) {
// $(args).replaceWith('<input type="text" />');
// }
</script>