Javascript Jquery-单击按钮后如何访问此元素
单击此按钮后如何访问此元素。。。 经过这么多的尝试,我还是不知道该怎么做。。。这是我的例子。。。因为我想尝试一下这个方法,但是用我自己的方式Javascript Jquery-单击按钮后如何访问此元素,javascript,jquery,Javascript,Jquery,单击此按钮后如何访问此元素。。。 经过这么多的尝试,我还是不知道该怎么做。。。这是我的例子。。。因为我想尝试一下这个方法,但是用我自己的方式 <div id="modalBodyContact" class="modal-body"> <table width="100%" cellspacing="0" class="display" id="contactsTable"> <tbody> <tr>
<div id="modalBodyContact" class="modal-body">
<table width="100%" cellspacing="0" class="display" id="contactsTable">
<tbody>
<tr>
<td>
<p data-id="5" class="contactInfo">123321</p>
</td>
<td class="pull-right">
<button data-id="5" class="edit" type="button">Edit</button>
<button data-id="5" class="delete" type="button">Delete</button>
</td>
</tr>
<tr>
<td>
<p data-id="6" class="contactInfo">123321</p> <---- I get this element after I click that EDIT BUTTON
</td>
<td class="pull-right">
<button data-id="6" class="edit" type="button">Edit</button> // <---- I CLICK THIS then ACCESS to <p>
<button data-id="6" class="delete" type="button">Delete</button>
</td>
</tr>
</tbody>
</table>
</div>
编辑1
这是小提琴
我想返回5555
而不是5555
。。。怎么做?因为我想用文本框替换它。[已编辑]
在函数“TBox”中,参数“obj”不是对象,而是字符串,因此需要传递对象而不是字符串。我再次更改了您的代码以接受对象,请查看
如果您想在按钮上生成onclick事件,则通过
this
获取其属性,然后执行以下操作
$("button.edit").on('click', function(){
var contact_id = $(this).attr("data-id");
var text = $(".contactInfo[data-id="+contact_id+"]").text();
alert("id is "+contact_id+" text is"+text);
});
请尝试以下代码:
$("#modalBodyContact").on('click', 'button.edit', function () {
console.log($(this).parents('tr').find("p[data-id='" + $(this).data("id") + "']").text());
});
您好,我想返回
5555
而不是5555
。。。怎么做?因为我想用文本框替换它…您好,我想返回5555而不是5555。。。怎么做?因为我想用文本框替换它..这就是你想要的吗?(顺便说一下,我已经编辑了答案)。等等。。。你知道我为什么会有``吗?我有空白。。。就像8个空格一样…在我的计算机上找不到id
,因为这些空格…在函数“TBox”中,参数“obj”不是对象,而是字符串,因此需要传递对象而不是字符串。我再次更改了您的代码以接受对象,请查看
$("button.edit").on('click', function(){
var contact_id = $(this).attr("data-id");
var text = $(".contactInfo[data-id="+contact_id+"]").text();
alert("id is "+contact_id+" text is"+text);
});
$("#modalBodyContact").on('click', 'button.edit', function () {
console.log($(this).parents('tr').find("p[data-id='" + $(this).data("id") + "']").text());
});