English 中文(简体)
设置点击发生的位置
原标题:Set where the click is going to happen

html:

<tr id="tr">
     <td>Somthing</td>
     <td>Somthing</td>
     <td>Somthing</td>
     <td>Somthing</td>
</tr>

滑动分页

$("#tr").click(function(){

// something happen

});

因此,问题是:

点击效果显然会得到<;tr>

当我点击<;tr>

编辑:

如果我有超过1 tr

<tr class="tr">
     <td>Somthing</td>
     <td>Somthing</td>
     <td>Somthing</td>
     <td>Somthing</td>
</tr>
<tr class="tr">
     <td>Somthing</td>
     <td>Somthing</td>
     <td>Somthing</td>
     <td>Somthing</td>
</tr>

the solution td:lt(4) will only work in the first tr ps.: i neeed to identify the tr, because i have another stuffs around the code working with tr s

我的真实代码是:

$(".class td:lt(6)").live("click", function(){


 });

damo:http://jsfiddle.net/w3hKf/4/

最佳回答

尝试使用lt(小于)选择器

$( .specialTD td:lt(4) ).click(function(){

// what cell was clicked?
var clickedId = $(this).attr( id );

});

更新:

您可以为所有行分配一个相同的类名,并为每个td分配一个单独的id

问题回答

您只能像这样将事件处理程序绑定到第三个元素:

$("#tr td:eq(2)").click(function(){

// somthing happen

});
$("tr#tr td:not(:last)").click(function(){

// somthing happen

});

是的,你可以!

JSfiddle DEMO

//########## using :lt() selector

$( #test1 li:lt(3) ).css({color:  #cf5 });

$( #test1 li:lt(3) ).click(function() {
    $(this).css({color:  #caf });
});

//########## using .slice() method for better performance

$( #test2 li ).slice(0, 3).css({color:  #cf5 });

$( #test2 li ).click(function() {
    $(this).slice(0, 3).css({color:  #caf });
});


"Because :lt() is a jQuery extension and not part of the CSS specification, queries using :lt() cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. For better performance in modern browsers, use $("your-pure-css-selector").slice(0, index) instead." (jQuery.com)

DOCUMENTATION:
http://api.jquery.com/lt-selector/
http://api.jquery.com/slice/





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.