2012-06-27 60 views

回答

6

只要幀訪問不受同源策略限制:

function getElem(selector, $root, $collection) { 
    if (!$root) $root = $(document); 
    if (!$collection) $collection = $(); 
    // Select all elements matching the selector under the root 
    $collection = $collection.add($root.find(selector)); 
    // Loop through all frames 
    $root.find('iframe,frame').each(function() { 
     // Recursively call the function, setting "$root" to the frame's document 
     getElem(selector, $(this).contents(), $collection); 
    }); 
    return $collection; 
} 
// Example: 
var $allImageElements = getElem('img');