2011-09-03 165 views
0

我有一個iframe和html文檔的fancynox與窗體。
我嘗試訪問表單的父DOM元素。 形式的ID是「形式」
我想:jQuery訪問DOM元素

$("#form").get(0).parentNode  
document.getElementsByTagName('iframe').getElementById('form') //TypeError 
document.getElementsByTagName('iframe').document //undefined 

如何訪問形式?

+0

檢查這個線程 - [如何獲得的iFrame使用javascript的src內容?] [1] [1]:http://gackoverflow.com/questions/1264569/how-to-get-iframe-src-content-using-javascript –

+0

'$(「#form」)'訪問表單元素,你需要做什麼? – NimChimpsky

+0

在窗體後追加一個子節點。我認爲$(「#form」)不是一個DOM節點。 – lvil

回答

1

$('#form')是你的情況。這是jQuery的節點,但是,所以你需要在其上使用jQuery功能...

參考jQuery的文檔上選擇更多的幫助和可用的API jQuery Docs

此外,如果你正在使用jQuery,你不需要做所有'getElementById'的東西,因爲你可以通過jQuery選擇器訪問所有東西。

//A couple selector examples 
$('#id') //By Id 
$('.class') //By Class 
$('tag') //By element tag 
$('#id').next() //Get next element 
$('#id').children() //list of child nodes 
$('#id').find('#id2') //element id2 somewhere inside id1 

爲了完整起見,這裏有一些API的例子

$('#id').css('background','red') //Change background to red 
$('#id').submit() //if id element is a form, submit it 
$('#id').val() //if id element is an input, get its value 
$('#id').text() //the text of an element 
$('#id').hide() //hide the element 
$('#id').show() //show the element