javascript - Finding the position of current td cell when click on -


The user is trying to find the existing cell that is clicking. For example if the user first clicked on the cell I have to return 1. If they click on the tenth cell then I want to come back. The table has 16 total rooms.

& lt; Html & gt; & Lt; Top & gt; & Lt; Title & gt; Lite out! & Lt; / Title & gt; & Lt; Script type = "text / javascript" src = "jquery-1.4.js" & gt; & Lt; / Script & gt; & Lt; Script type = "text / javascript" & gt; // var / i } Function getTdCount () {return $ ("TD") Length; } $ (Document) .ready (function () {$ ("#btr tr td"). Hover (function () {$ (this) .Css ('border-color', '00FF33');}, function) $ $ (This). CSS ('border color', 'black')}) $ offProtoType = $ ('# offprototype') CSS ('display', 'block'). RemoveAttr ('id') $ ('TD'). App ($ Offproto Type) $ ("#btr tr td") Click (function () {$ ("img", this) .attr ('src', 'on.png')})}); & Lt; / Script & gt; & Lt; Style type = "text / css" & gt; TD {Border-style: Solid; Border-color: black; Background color: black; Swim left; } & Lt; / Style & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; Img style = "display: none" id = "offprototype" src = "off.png" & gt; & Lt; Img style = "display: none" id = "onprototype" src = "on.png" & gt; & Lt; Table id = "board" limit = "3" bgcolor = "black" align = "center" & gt; & Lt; TR & gt; & Lt; TD & gt; & Lt; / TD & gt; & Lt; TD & gt; & Lt; / TD & gt; & Lt; TD & gt; & Lt; / TD & gt; & Lt; TD & gt; & Lt; / TD & gt; & Lt; / TR & gt; & Lt; TR & gt; & Lt; TD & gt; & Lt; / TD & gt; & Lt; TD & gt; & Lt; / TD & gt; & Lt; TD & gt; & Lt; / TD & gt; & Lt; TD & gt; & Lt; / TD & gt; & Lt; / TR & gt; & Lt; TR & gt; & Lt; TD & gt; & Lt; / TD & gt; & Lt; TD & gt; & Lt; / TD & gt; & Lt; TD & gt; & Lt; / TD & gt; & Lt; TD & gt; & Lt; / TD & gt; & Lt; / TR & gt; & Lt; TR & gt; & Lt; TD & gt; & Lt; / TD & gt; & Lt; TD & gt; & Lt; / TD & gt; & Lt; TD & gt; & Lt; / TD & gt; & Lt; TD & gt; & Lt; / TD & gt; & Lt; / TR & gt; & Lt; / Table & gt; & Lt; Input type = "button" value = "drag" onclick = "change ()" /> & Lt; / Body & gt; & Lt; / Html & gt;

Use:

  var $ tds = $ ('#tr tt'); $ Tds.click (function () {// based index becomes, and 1: var num = $ tds.index (this) + 1; $ ("img", this) .attr ('src', ' On.png ')});  

We first cache the results of #board tr td search, so we do not want to see it on every click.

Next, we use the index function to find the current td index with the complete result of td elements .

Update: jQuery 1.4

This is not an example that we are new to jQuery You can use the index () feature. Without reference to the index () parameter, the index of the element gets from the context of its siblings. In this case, we need it in reference to all the td elements, not That only to his siblings

If no argument argument is passed. The index () method, return value is an integer, which shows the position of the first element within the jQuery object relative to its brother elements.


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -