2012-12-13 37 views
1

VB腳本語句,如何使用javascript集中表格單元格?

Set oHighlightedRow = document.all("SearchRow" & nHighlightedRow) oHighlightedRow.cells(0).focus()

這兩個statments需要轉換到javascript.anyone可以幫我找到解決的辦法? 感謝名單

我轉換的代碼是,

var oHighlightedRow = $("#SearchRow" + nHighlightedRow); 
oHighlightedRow.cells[0].focus(); 

這是正確的嗎?

回答

4

OK:

var oHighlightedRow = document.all("SearchRow" + nHighlightedRow); 
oHighlightedRow.cells[0].focus(); 

或者,更好的(假設行都有"SearchRow" + nHighlightedRow一個id):

var oHighlightedRow = document.getElementById("SearchRow" + nHighlightedRow); 
oHighlightedRow.cells[0].focus(); 

或者,jQuery的(再次假設該行有"SearchRow" + nHighlightedRow一個id):

$("#SearchRow" + nHighlightedRow + " td:first").focus(); 
+0

感謝名單。 –

2

你不能集中表格單元格在所有瀏覽器。這裏是jQuery的文件說什麼:

The focus event is sent to an element when it gains focus. This event is implicitly applicable to a limited set of elements, such as form elements (<input>, <select>, etc.) and links (<a href>). In recent browser versions, the event can be extended to include all element types by explicitly setting the element's tabindex property. An element can gain focus via keyboard commands, such as the Tab key, or by mouse clicks on the element.

爲了確保這適用於所有的瀏覽器就可以實現一些CSS類,並添加事件偵聽鼠標鍵。然後,添加/刪除表格單元格中的css類。

爲了讓你回答焦點的元素與id="target"使用該

 
$('#target').focus(); 
相關問題