2016-06-21 202 views
1

我希望將下一個元素的tabindex值分配給它的前一個元素,當我嘗試運行此代碼時未定義變量el。如何獲取具有屬性tabindex的下一個元素

if(event.keyCode == 9){ 
    $(this).removeAttr('readonly'); 
    var el = $(this).closest('input, select').attr('tabindex'); 
    var tV = el.val(); 
    if(el != ' ' || el != null){ 
     $(this).attr('tabindex', tV - 1); 
    } 
    else{ 

    } 
} 
+2

向我們提供您的HTML請 –

+0

$(這)是指jQueryUI的日期選擇器元素.. –

+0

如果你想下一個元素,只是做'變種EL = $(本)。接下來().attr('tabindex');' –

回答

2

您正在挑選下一個input/select元素並獲取其tabindex。如果沒有定義tabindex,則獲得undefined

試試這個:

$(this).nextAll("[tabindex]:first").attr('tabindex'); 
1

那麼它與document.querySelector() API非常基本的選擇邏輯。假設我們有mainElement,並且我們想要獲得mainElement下的tabindex = -1屬性和值的下一個元素。

讓我們看看;

var mainElement = document.querySelector('#main'), 
 
inputWithTabIndex = mainElement.querySelector('[tabindex = "-1"]'); 
 
console.log(inputWithTabIndex); 
 
setTimeout(function() { 
 
      inputWithTabIndex.setAttribute("tabindex","0"); 
 
      console.log(inputWithTabIndex); 
 
      },1000);
<div id="main"> 
 
    <div class="inputContainer"> 
 
    <input type="text" placeholder="Type here"> 
 
    </div> 
 
    <div class="inputContainer"> 
 
    <input type="text" placeholder="Type here"> 
 
    </div> 
 
    <div class="inputContainer"> 
 
    <input type="text" placeholder="Type here" tabindex = "-1"> 
 
    </div> 
 
    <div class="inputContainer"> 
 
    <input type="text" placeholder="Type here"> 
 
    </div> 
 
</div>

+0

我們不能通過使用tabindex的值而不是觸發其前一個/下一個元素的tabindex屬性來做同樣的事情,如果沒有爲我想要的下一個元素定義tabindex跳過它並檢查下一個。 –

+0

@Hiteshkumar Misro檢查一下http://stackoverflow.com/a/11475290/4543207 – Redu

相關問題