Javascript 如何选择正确的div
我需要确定按钮是在哪个div中单击的,这是ajax函数,我需要在php标记中回显num,但是我只希望它在类/帖子中回显,因为它在while循环中,并且有许多按钮的副本Javascript 如何选择正确的div,javascript,jquery,html,css,ajax,Javascript,Jquery,Html,Css,Ajax,我需要确定按钮是在哪个div中单击的,这是ajax函数,我需要在php标记中回显num,但是我只希望它在类/帖子中回显,因为它在while循环中,并且有许多按钮的副本 while ($row1 = mysql_fetch_array($event1)) { $event = $row1['post']; $timeposted = $row1['date']; $eventmemdata = mysql_query("SELECT id,firstname FROM users
while ($row1 = mysql_fetch_array($event1))
{
$event = $row1['post'];
$timeposted = $row1['date'];
$eventmemdata = mysql_query("SELECT id,firstname FROM users WHERE id = '$id' LIMIT 1");
while($rowaa = mysql_fetch_array($eventmemdata))
{
$name = $rowaa['firstname'];
$eventlist = "$event <br> $name";
}
echo " <div id = 'eventer'> $timeposted <br>$eventlist</div> <input name='myBtn' type='submit' value='increment' onClick='javascript:ajax_post();'>
<input name='lol' type='submit' value='dec' onClick='javascript:ajax_posta();'>
<div id = 'status'>lol</div>";
echo "<br>";
}
?>
那么如何在单击按钮的特定Div中显示信息/数据。正如我现在看到的,回声发生在它拾取的第一节课上!
非常感谢,谢谢 若我理解正确,你们有多个按钮可以按下,你们想知道巫婆被按下了 将其添加到onclick事件中
<script language="JavaScript" type="text/javascript">
var num = 1;
function ajax_post(){
// Create our XMLHttpRequest object
var hr = new XMLHttpRequest();
// Create some variables we need to send to our PHP file
var url = "javas.php";
hr.open("POST", url, true);
// Set content type header information for sending url encoded variables in the request
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById("status").innerHTML = return_data;
}
}
// Send the data to PHP now... and wait for response to update the status div
hr.send("num=" + (++num)); // Actually execute the request
document.getElementById("status").innerHTML = "processing...";
}
或者,您可以在将属性发送到函数之前获取所需的属性
如果您有firefox+firebug,请使用此选项查看对象的所有属性,并获取所需内容:
function(){ whateverFunctionYouUsed(this)}
使用jQuery非常简单:
function(){
console.log(this)
whateverFunctionYouUsed(this)
}
不确定为什么有jQuery标记,您提供的代码中显然没有可用的jQuery。@不过,他们可能对使用jQuery的答案感兴趣。
$("yourContainerId > div").click(function () {
var index = $("div").index(this);
alert(index);
});