0

ページからビデオプレーヤーを削除し、必要に応じて元に戻すコードを作成しています (要素に ID がない場合でも)。私は IE7 で問題を見つけています。私のコードは次のとおりです。

var weboElem, weboElemPar, weboElemIndex, weboStored;
function weboRemoveVideoplayer(vpId){
    weboElem = document.getElementById(vpId);
    if(!weboElem) return false;
    weboElemPar = weboElem.parentNode;
    weboElemIndex = 0;
    var child = weboElem;
    while( (child = child.previousSibling) != null ) 
      weboElemIndex++;
    weboElemPar.removeChild(weboElem);
    return true;
}
function weboPlaceVideoplayerBack(){
    if(weboElemPar.insertBefore !== undefined && weboElemPar.childNodes !== undefined)
    {
        weboElemPar.insertBefore(weboElem, weboElemPar.childNodes[weboElemIndex]);
        return true;
    }
    return false;   
}

var result = document.evaluate(     
    '//*/param[contains(@value, "autoplay=1")]/..', // XPath expression 
    document, // context node
    null, // namespace resolver
    XPathResult.ORDERED_NODE_SNAPSHOT_TYPE
);

if(result.snapshotLength > 0)
{
    var node = result.snapshotItem(0);
    node.id = "webo";
    document.getElementById('info').innerHTML = node.nodeName.toLowerCase()+" -> "+node.id;
} else document.getElementById('info').innerHTML = "not found";

(javascript-xpath ライブラリをインポートしたため、document.evaluate が機能することに注意してください) IE7 では、XPath が IFRAME を見つけた場合は問題なく動作しますが、OBJECT が見つかった場合は何もweboElem = document.getElementById(vpId);せず、ID が見つからなかったかのように停止します。 .

次のようにコードを変更してみました。

if(result.snapshotLength > 0)
{
    var node = result.snapshotItem(0);
    node.id = "webo";
    node.parentNode.removeChild(node);
    document.getElementById('info').innerHTML = node.nodeName.toLowerCase()+" -> "+node.id;
    if(node.nodeName.toLowerCase() == "object") weboStored = node;
    else weboStored = null;
} else document.getElementById('info').innerHTML = "not found";

それが機能すると、ビデオプレーヤーはページの読み込み時に消えます。私は関数を使用したいので、すべてを次のように編集しました (後で weboRemoveVideoplayer 関数で取得するグローバル var にノードを格納します)。

var weboElem, weboElemPar, weboElemIndex, weboStored;
function weboRemoveVideoplayer(vpId){
    if(!weboStored) weboElem = document.getElementById(vpId);
    else weboElem = weboStored;
    if(!weboElem) return false;
    weboElemPar = weboElem.parentNode;
    weboElemIndex = 0;
    var child = weboElem;
    while( (child = child.previousSibling) != null ) 
      weboElemIndex++;
    weboElemPar.removeChild(weboElem);
    alert("5");
    return true;
}
function weboPlaceVideoplayerBack(){
    if(weboElemPar.insertBefore !== undefined && weboElemPar.childNodes !== undefined)
    {
        weboElemPar.insertBefore(weboElem, weboElemPar.childNodes[weboElemIndex]);
        return true;
    }
    return false;   
}

// bind XPath methods to document and window objects
// NOTE: This will overwrite native XPath implementation if it exists
//XPathJS.bindDomLevel3XPath(); //solo per xpathJs
var result = document.evaluate( 
    '//*/param[contains(@value, "autoplay=1")]/..', // XPath expression 
    document, // context node
    null, // namespace resolver
    XPathResult.ORDERED_NODE_SNAPSHOT_TYPE
);

if(result.snapshotLength > 0)
{
    var node = result.snapshotItem(0);
    node.id = "webo";
    node.parentNode.removeChild(node);
    document.getElementById('info').innerHTML = node.nodeName.toLowerCase()+" -> "+node.id;
    if(node.nodeName.toLowerCase() == "object") weboStored = node;
    else weboStored = null;
} else document.getElementById('info').innerHTML = "not found";

このようにして、コードは親ノードを取得しようとするとブロックされます。

誰かがここで何をすべきか教えてもらえますか?

PS: chrome と firefox を使用すると、最初に投稿したバージョンでコードが完全に機能します。

4

1 に答える 1

0

修正しました!必要なときにいつでも取得できる自分で選択したIDを使用して、オブジェクトをdiv内にラップすることで問題を解決しました。これはresolveXpath関数で行います。

ここにコードがあります:

var weboElem, weboElemPar, ieObject = false;
var weboElemIndex = 0;
function weboRemoveVideoplayer(vpId){
    var child;      
    if(!ieObject) weboElem = document.getElementById(vpId);
    else weboElem = document.getElementById('my_usage');
    if(!weboElem) return false;
    weboElemPar = weboElem.parentNode;
    weboElemIndex = 0;
    child = weboElem;
    while( (child = child.previousSibling) != null ) weboElemIndex++;
    if(typeof weboElemPar.removeChild !== 'undefined') weboElemPar.removeChild(weboElem);
    else return false;

    return true;
}
function weboPlaceVideoplayerBack(){
    if(typeof weboElemPar.insertBefore !== 'undefined' && typeof weboElemPar.childNodes !== 'undefined' && typeof weboElemPar.appendChild !== 'undefined'){
        if(weboElemPar.childNodes.length > 0 && weboElemIndex < weboElemPar.childNodes.length) weboElemPar.insertBefore(weboElem, weboElemPar.childNodes[weboElemIndex]);
        else weboElemPar.appendChild(weboElem);
        return true;
    }
    return false;   
}
function resolveXpath(path)
{
    //XPathJS.bindDomLevel3XPath(); //solo per xpathJs
    var result = document.evaluate(path,document,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);      
    if(result.snapshotLength > 0){
        var child, node = result.snapshotItem(0);
        if(node.nodeName.toLowerCase() == 'object'){
            ieObject = true;        
            child = node;
            while( (child = child.previousSibling) != null ) weboElemIndex++;
            var div = document.createElement('div');
            div.id = 'my_usage';        
            if(typeof node.parentNode.insertBefore !== 'undefined' && typeof node.parentNode.childNodes !== 'undefined' && typeof node.parentNode.appendChild !== 'undefined'){
                if(node.parentNode.childNodes.length > 0 && weboElemIndex < node.parentNode.childNodes.length) node.parentNode.insertBefore(div,node.parentNode.childNodes[weboElemIndex]);
                else node.parentNode.appendChild(div);
                div.appendChild(node);
            } else return false;
        } else node.id = 'my_usage';            
        return true;
    } else return false;
}
resolveXpath('//*/param[contains(@src, "autoplay=1")]/..');
于 2013-02-20T15:15:20.733 に答える