How to catch all value on a single click?
<tr>
<td>x</td>
<td>y</td>
<td>z</td>
<td> href </td>
</tr>
On clicking the href
in last <td>
I 开发者_开发技巧want to catch the value to show these values in the pop up
. How can I catch these values?
<script>
functionn showValues()
{
var response;
response = document.getElementById("x").innerHTML;
response += document.getElementById("y").innerHTML;
response += document.getElementById("z").innerHTML;
window.alert(response);
}
</script>
<tr>
<td id="x">x</td>
<td id="y">y</td>
<td id="z">z</td>
<td onclick="showValues()"> href </td>
</tr>
- Create a function that will run onclick. onclick is the handler for click events in javascript.
- Grab the innerHTML of the elements you want.
- Alert the user.
<tr>
<td><span id="v1">x</span></td>
<td>y</td>
<td>z</td>
<td> href </td>
</tr>
specify span and give id to it and extract the value using document.getElementByID().innerHTML; same for all other rows in javascript
try this
<script>
function test()
{
var arrCells = document.getElementsByTagName('td');
for (idx=0;idx<arrCells.length-1;idx++) {
strValue = arrCells[idx].innerHTML;
alert(strValue);
}
}
</script>
<table>
<tr>
<td>x</td>
<td>y</td>
<td>z</td>
<td><a href="#" onclick="return test()">href </a></td>
</tr>
</table>
you can use siblings() to do it.
$("tr td:last").click(function(){
alert($(this).siblings().text());
});
<tr>
<td>x</td>
<td>y</td>
<td>z</td>
<td> <a href="#" onclick="catchAll(this)" >clickhere </a><</td>
</tr>
function catchAll(obj)
{
var tdNodes= obj.parentNode.parentNode.childNodes;
}
You can using for loop to trace tdNodes .
$("td:last").click(function(){
val_of_all_td=[];
$("td").each(function(){
val_of_all_td.push($('this').html());
});
});
Array val_of_all_td
contains all the value of td.
Download jquery and insert it in your script jquery
You can catch your values just one click with DOM Traversal method (contains):
$('td:contains(href)').click( function(){
str= " ";
$('td').each( function(){
str+= $(this).text();
});
alert(str); // u can change it with your script to display it on pop up
});
精彩评论