if (((document.activeElement).parentNode).tagName == "div") {
//do amazing things
}
由於某種原因我的特定功能無法正常工作,我認爲它與上面的行有關。上面的語句是語句正確還是錯誤?如果上面的行是正確的,我將刪除該問題(因爲那麼問題是無用的),或者添加其他功能的附加信息。正確使用.tagName
感謝
if (((document.activeElement).parentNode).tagName == "div") {
//do amazing things
}
由於某種原因我的特定功能無法正常工作,我認爲它與上面的行有關。上面的語句是語句正確還是錯誤?如果上面的行是正確的,我將刪除該問題(因爲那麼問題是無用的),或者添加其他功能的附加信息。正確使用.tagName
感謝
標記名給出了大寫的標記,以便它會給DIV
見https://developer.mozilla.org/en-US/docs/DOM/element.tagName
您可以使用.toLowerCase() == 'div'
(如在評論中所建議的)來消除的情況下的問題。
試試這個
if (((document.activeElement).parentNode).tagName === "DIV") {
//do amazing things
}
你並不需要所有的括號。爲了安全起見,請使用tagName.toLowerCase()
。
if (document.activeElement.parentNode.tagName.toLowerCase() == "div") {
//do amazing things
}
可能有一些瀏覽器有時不會給出與其他情況相同的情況。這確保你總是比較相同的情況。
ty說你不需要所有的parens,不知道是不是。 – 2012-08-15 22:24:45
由tagName返回的HTML值始終爲大寫。但是如果你想在XML/XHTML中運行這個腳本,它將返回標記的名稱,因爲它不一定是大寫。嘗試撥打document.activeElement.parentNode.tagName.toLowerCase()
DIV或「DIV」?只是想知道 – 2012-08-15 22:21:06
@Hubrid:'tagName'是一個字符串,所以''DIV''。您也可以將其轉換爲小寫:'.tagName.toLowerCase()==='div''。 – 2012-08-15 22:21:35
tys,upvotes,獎勵這些用戶! – 2012-08-15 22:22:19