顯示具有 jQuery 標籤的文章。 顯示所有文章
顯示具有 jQuery 標籤的文章。 顯示所有文章

2013年6月2日 星期日

jQuery parent() && children() ( Select input from next td )

html:
<tr>
    <td>
        <input type="text" id="txtPrev" />
    </td>
    <td>
        <input type="text" id="txtNext" />
    </td>
</tr>
找到下一個 td 裡的 input:
var flag = 0;
$("input").keypress(function(){
    var target = $(this).parent("td").next().children("input");
    if(flag%2)
        target.css("background-color", "blue");
    else
        target.css("background-color", "green");
    ++flag;
});

Demo

jQuery 關閉功能鍵 ( Disable Function Key )

function disableFKey(e){
    if( (e.which || e.keyCode) == 116 ){         // F5
        e.preventDefault();
    }else if( (e.which || e.keyCode) == 120 ){   // F9
        e.preventDefault();
        location.href = 'www.google.com'  // do something
    }
});

$(document).on("keydown", disableFKey);

2012年11月28日 星期三

jQuery 選擇器 ( Selector )

變更input欄位的值:
$("input").val("newText");
變更選取id的值:
$("#myId").val("newText");
變更id:
$("#myId").attr("id", "newId");
變更class:
$("#myId").attr("class", "newClass");
移除class:
$("#myId").removeClass("myClass");
取得第n個元素:
$("tr:eq(2)")  // 取得第3個tr 
使用變數:
var index = 5;
$("input[type=text]:eq("+index+")")  // 取得第6個textbox 變數必須以雙引號包
取得前n個元素:
$("a").slice(0, 10) // 取前10個a 
class篩選:
$("tr[class!='pager']") // 取得class不是pager的tr 

2012年10月10日 星期三

jQuery 在 table 裡失效

想做一個讓使用者依序輸入的表格,
在 stackoverflow 上問到方法後,
自己的page卻不能作用,但在 jsfiddle 上可以。( http://jsfiddle.net/gionaf/wLnh7/)
結果原來是 html 的<td>標籤
讓它失效。