jQuery 正确更改了源代码,但未更改渲染的页面结果。 (有jsFiddle证明)

发布于 2024-12-12 10:00:17 字数 2508 浏览 4 评论 0原文

这是我的 jQuery:

$("object").each(function(){
    video_url  = $(this).find('embed').attr('src');
    new_video_url = video_url+"&autohide=1&modestbranding=1&showinfo=0&theme=light";
    $(this).find('embed').attr('src', new_video_url);
    $(this).find('param:eq(0)').val(new_video_url);
    $(this).find('embed').before('<param name="autoplay" value="1"><param name="showinfo" value="1"><param name="modestbranding" value="1"><param name="hd" value="1"><param name="color" value="white"><param name="autohide" value="1">');
});

这是 jQuery 之前的代码的样子:

<object width="500" height="371">
    <param name="movie" value="http://www.youtube.com/v/ISeQBRUa8xQ&amp;rel=0&amp;egm=0&amp;showinfo=0&amp;fs=1">
    <param name="wmode" value="transparent">
    <param name="allowFullScreen" value="true">
    <embed 
        src="http://www.youtube.com/v/ISeQBRUa8xQ&amp;rel=0&amp;egm=0&amp;showinfo=0&amp;fs=1" 
        type="application/x-shockwave-flash" 
        width="500" 
        height="371" 
        allowfullscreen="true" 
        mode="transparent"
    >
</object>

这是执行 jquery 之后的样子。

<object width="500" height="371">
    <param name="movie" value="http://www.youtube.com/v/ISeQBRUa8xQ&rel=0&egm=0&showinfo=0&fs=1&autohide=1&modestbranding=1&showinfo=0&theme=light">
    <param name="wmode" value="transparent">
    <param name="allowFullScreen" value="true">
    <param name="autoplay" value="1">
    <param name="showinfo" value="1">
    <param name="modestbranding" value="1">
    <param name="hd" value="1">
    <param name="color" value="white">
    <param name="autohide" value="1">
    <embed 
        src="http://www.youtube.com/v/ISeQBRUa8xQ&rel=0&egm=0&showinfo=0&fs=1&autohide=1&modestbranding=1&showinfo=0&theme=light" 
        type="application/x-shockwave-flash" 
        width="500" 
        height="371" 
        allowfullscreen="true" 
        wmode="transparent"
        >
</object>

即使 HTML 代码已成功更改,页面的呈现也不会显示更新后的代码。

这是证明该问题的 jsFiddle: http://jsfiddle.net/4kQYy/4/

如果您检查检查器中的第二个 YouTube 视频,您会发现它实际上 100% 正确地注入了代码,但页面渲染并未反映更新。怎么可能呢?您在源代码中看到的肯定就是您在屏幕上看到的吗?帮助!

Here is my jQuery:

$("object").each(function(){
    video_url  = $(this).find('embed').attr('src');
    new_video_url = video_url+"&autohide=1&modestbranding=1&showinfo=0&theme=light";
    $(this).find('embed').attr('src', new_video_url);
    $(this).find('param:eq(0)').val(new_video_url);
    $(this).find('embed').before('<param name="autoplay" value="1"><param name="showinfo" value="1"><param name="modestbranding" value="1"><param name="hd" value="1"><param name="color" value="white"><param name="autohide" value="1">');
});

Here is what the code looked like before the jQuery:

<object width="500" height="371">
    <param name="movie" value="http://www.youtube.com/v/ISeQBRUa8xQ&rel=0&egm=0&showinfo=0&fs=1">
    <param name="wmode" value="transparent">
    <param name="allowFullScreen" value="true">
    <embed 
        src="http://www.youtube.com/v/ISeQBRUa8xQ&rel=0&egm=0&showinfo=0&fs=1" 
        type="application/x-shockwave-flash" 
        width="500" 
        height="371" 
        allowfullscreen="true" 
        mode="transparent"
    >
</object>

Here is what it looks like after the jquery has been executed.

<object width="500" height="371">
    <param name="movie" value="http://www.youtube.com/v/ISeQBRUa8xQ&rel=0&egm=0&showinfo=0&fs=1&autohide=1&modestbranding=1&showinfo=0&theme=light">
    <param name="wmode" value="transparent">
    <param name="allowFullScreen" value="true">
    <param name="autoplay" value="1">
    <param name="showinfo" value="1">
    <param name="modestbranding" value="1">
    <param name="hd" value="1">
    <param name="color" value="white">
    <param name="autohide" value="1">
    <embed 
        src="http://www.youtube.com/v/ISeQBRUa8xQ&rel=0&egm=0&showinfo=0&fs=1&autohide=1&modestbranding=1&showinfo=0&theme=light" 
        type="application/x-shockwave-flash" 
        width="500" 
        height="371" 
        allowfullscreen="true" 
        wmode="transparent"
        >
</object>

Even though the HTML code has successfully changed, the rendering of the page does not show the updated code.

Here is a jsFiddle proving the issue: http://jsfiddle.net/4kQYy/4/

If you inspect the second youtube video in your inspector, you'll see that it actually gets the code injected 100% correctly, yet the page render does not reflect the update. How can that be? Surely what you see in source code is what you see on screen? Help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过度放纵 2024-12-19 10:00:17

我不是闪存专家,但我的逻辑是,你想做的事情是行不通的。在我看来,会发生以下步骤:

  1. 初始化 Flash 实例
  2. 在初始化时,Flash 影片获取参数并适当地执行操作
  3. 然后更改参数,这些参数不再对 Flash 影片产生任何影响

您可以做的就是移动整个 Flash 实例flash实例化过程到javascript。这意味着您生成了整个对象标签。或者您分离对象标签,进行自定义并再次重新插入。我更新了您的 fiddle 中的代码

$("#foobar").each(function(){
    var parent = $(this).parent(),
        object = $(this).detach()

    video_url  = object.find('embed').attr('src');
    new_video_url = video_url+"&autohide=1&modestbranding=1&showinfo=0&theme=light";
    object.find('embed').attr('src', new_video_url);
    object.find('param:eq(0)').val(new_video_url);
    object.find('embed').before('<param name="autoplay" value="1"><param name="showinfo" value="1"><param name="modestbranding" value="1"><param name="hd" value="1"><param name="color" value="white"><param name="autohide" value="1">');
    parent.append(object);
});

我知道这并不适用于所有情况,特别是如果父母有多个孩子,但它应该让您有一个大概的了解。

I'm not a flash expert but my logic says, that what you are trying to do can not work. What happens in my opinion are the following steps:

  1. You initialize a flash instance
  2. On initialization the flash movie gets the parameters and acts apropriatly
  3. Afterwards you change the params, which have no effect on the flash movie anymore

What you could do is either move the whole flash instantiation process to javascript. That means you generate the whole object tag. Or you detach the object tag, make your customizations and reinsert it again. I updated the code in your fiddle

$("#foobar").each(function(){
    var parent = $(this).parent(),
        object = $(this).detach()

    video_url  = object.find('embed').attr('src');
    new_video_url = video_url+"&autohide=1&modestbranding=1&showinfo=0&theme=light";
    object.find('embed').attr('src', new_video_url);
    object.find('param:eq(0)').val(new_video_url);
    object.find('embed').before('<param name="autoplay" value="1"><param name="showinfo" value="1"><param name="modestbranding" value="1"><param name="hd" value="1"><param name="color" value="white"><param name="autohide" value="1">');
    parent.append(object);
});

I know that this does not work in every condition especially, if the parent has more than one child, but it should give you a general idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文