How i can insert a button in cell of table using jquery and process onclick event for 开发者_如何转开发that button?
Thank you.
var button = $("<button>Hi there</button>");
button.click(function() {
alert("Hi back!");
});
button.appendTo("#tablecell");
http://jsfiddle.net/Fxayf/1/
<script>
function insert(button){
$(button).appendTo($('td.toinsert'));
}
</script>
<table>
<tr>
<td class="toinsert">inside cell</td>
</tr>
</table>
<input type="button" onclick="insert(this)" value="button"/>
Also for creating button and adding event to it
<script>
$(function(){
$('<input></input>').attr({'type': 'button'}).val("button").click(function(){
alert('hello');
}).appendTo($('td.toinsert'));
});
</script>
<table>
<tr>
<td class="toinsert">inside cell</td>
</tr>
</table>
精彩评论