7

我有一个显示来自 issuu.com 的动态 Flash 内容的页面。我需要添加wmode="transparent",否则导航菜单会显示在 Flash 下。有没有用 jQuery 或简单的 java 脚本来做这件事的捷径?

我不想每次添加闪存时都更改嵌入代码。

4

6 回答 6

11

好的,在网上搜索了 2 天的答案后,我发现了一个纯 JS 函数,可以在所有浏览器中修复它!

你去:

function fix_flash() {
    // loop through every embed tag on the site
    var embeds = document.getElementsByTagName('embed');
    for (i = 0; i < embeds.length; i++) {
        embed = embeds[i];
        var new_embed;
        // everything but Firefox & Konqueror
        if (embed.outerHTML) {
            var html = embed.outerHTML;
            // replace an existing wmode parameter
            if (html.match(/wmode\s*=\s*('|")[a-zA-Z]+('|")/i))
                new_embed = html.replace(/wmode\s*=\s*('|")window('|")/i, "wmode='transparent'");
            // add a new wmode parameter
            else
                new_embed = html.replace(/<embed\s/i, "<embed wmode='transparent' ");
            // replace the old embed object with the fixed version
            embed.insertAdjacentHTML('beforeBegin', new_embed);
            embed.parentNode.removeChild(embed);
        } else {
            // cloneNode is buggy in some versions of Safari & Opera, but works fine in FF
            new_embed = embed.cloneNode(true);
            if (!new_embed.getAttribute('wmode') || new_embed.getAttribute('wmode').toLowerCase() == 'window')
                new_embed.setAttribute('wmode', 'transparent');
            embed.parentNode.replaceChild(new_embed, embed);
        }
    }
    // loop through every object tag on the site
    var objects = document.getElementsByTagName('object');
    for (i = 0; i < objects.length; i++) {
        object = objects[i];
        var new_object;
        // object is an IE specific tag so we can use outerHTML here
        if (object.outerHTML) {
            var html = object.outerHTML;
            // replace an existing wmode parameter
            if (html.match(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")[a-zA-Z]+('|")\s*\/?\>/i))
                new_object = html.replace(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")window('|")\s*\/?\>/i, "<param name='wmode' value='transparent' />");
            // add a new wmode parameter
            else
                new_object = html.replace(/<\/object\>/i, "<param name='wmode' value='transparent' />\n</object>");
            // loop through each of the param tags
            var children = object.childNodes;
            for (j = 0; j < children.length; j++) {
                try {
                    if (children[j] != null) {
                        var theName = children[j].getAttribute('name');
                        if (theName != null && theName.match(/flashvars/i)) {
                            new_object = new_object.replace(/<param\s+name\s*=\s*('|")flashvars('|")\s+value\s*=\s*('|")[^'"]*('|")\s*\/?\>/i, "<param name='flashvars' value='" + children[j].getAttribute('value') + "' />");
                        }
                    }
                }
                catch (err) {
                }
            }
            // replace the old embed object with the fixed versiony
            object.insertAdjacentHTML('beforeBegin', new_object);
            object.parentNode.removeChild(object);
        }
    }
}

现在您可以在页面使用 jQuery 加载时运行:

 $(document).ready(function () {
      fix_flash();
      // Call Function
 }); 
于 2010-11-26T00:21:52.207 回答
8

你可以使用这个 Jquery 代码:

$("object[type='application/x-shockwave-flash']").append('<param name="wMode" value="transparent"/>');
于 2010-04-24T12:49:18.360 回答
3

我无法让 Mannaz 版本正常工作,并且想要一个更简洁的 jQuery 函数来将元素重新写入页面,所以写了一个新函数:

    function fixFlash() {
        $('object').each(function(){
            $(this).find('embed').attr('wmode','transparent');
            if($(this).find('param[name=wmode]').attr('value') != 'transparent') {
                $(this).prepend('<param name="wmode" value="transparent" />');
            }
            temp_var = $(this).parent().html();
            $(this).parent().html(temp_var);
        });
    }
    // Put function execute into onload area
    fixFlash();
于 2012-07-11T15:42:36.377 回答
2

来自stackoverflow中的另一篇文章,使用JQuery这行得通

$("embed").attr("wmode", "opaque").wrap('<div>');

I use this when using a FCKeditor youtube plugin, as the plugin doesn't wrap the embed node with a object node.

  $('embed').each(function(){
    if($(this).parent()[0]['tagName'] !='OBJECT'){
      $(this).attr("wmode", "opaque").wrap('<div>');
    }
  });
于 2010-11-18T20:45:27.137 回答
1

上次我检查时,通过 JS 更改 wmode 不起作用。一旦创建了 Flash 对象,更改其 wmode 值就没有实际效果。

您需要从后端执行此操作,或者如果不可能,则在 JS BUT 中执行此操作,然后再编写对象。

高温高压

于 2010-04-25T08:42:00.767 回答
1

这是我能找到的最简单且与浏览器兼容的解决方案,以便在页面已经呈现后删除 wMode="window" 对象。

在 Internet Explorer 中,对象的子节点一旦插入 DOM 就无法修改。最好的解决方案是通过修改 outerHTML 来重新插入/重新渲染它。

在大多数其他浏览器中,修改对象/嵌入的属性是可能的,但一旦呈现就没有效果。在这里,在相同位置重新插入元素会导致重新渲染。.wrap().unwrap() 是我在 jQuery 中想到的最简单的方法。

$("embed[wmode=window], embed:not([wmode])").attr("wmode", "opaque").wrap("<div/>").unwrap();
$("object:has(param[name=wmode][value=window]), object:not(:has(> param[name=wmode]))").each(object);

function object() {
  this.outerHTML = this.outerHTML.replace(/<(?:[^">]+|(["']).*?\1)*>/, '$&<param name="wmode" value="opaque"/>');
}
于 2012-08-03T19:01:18.787 回答