为什么即使在此代码中,脚本也会影响我的 Rails 3 应用程序上的所有内容? [英] why does the script affect everything on my Rails 3 app even when cased in this code?

查看:27
本文介绍了为什么即使在此代码中,脚本也会影响我的 Rails 3 应用程序上的所有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个第三方脚本

<script type="text/javascript" src="http://thirdpartysite.com/front.asp?id=xxxx"></script>

脚本的作用是在图像上添加水印以表明它是受版权保护的.

这是我在视图中使用的代码,但无论如何,该脚本适用于所有帖子

<% if post.copyright == true %><script type="text/javascript" src="http://thirdpartysite.com/front.asp?id=xxxx"></script><%其他%><%结束%>

当我仅使用文本对其进行测试时,它可以正常工作

<% if post.copyright == true %>此处的示例文本仅适用于 copyright==true 的帖子<%其他%><%结束%>

如何让脚本仅适用于某些帖子?

解决方案

在你的评论中澄清后

是的,如果 javascript 中没有指定其他内容,则包括 javascript 将在整个页面范围内应用.

您需要执行以下操作:

<img src="somesrc.jpg"></img>

然后仅将 javascript 应用于 css 选择器div.copyright img".类似的东西.取决于 javascript 支持什么,或者您是否可以更改源.

I have a third party script which is

<script type="text/javascript" src="http://thirdpartysite.com/front.asp?id=xxxx"></script>

What the script does is put a watermark on an image to show that it is copyrighted.

This is the code that I'm using in view, but no matter what, the script applies to all posts

<% if post.copyright == true %>      
    <script type="text/javascript" src="http://thirdpartysite.com/front.asp?id=xxxx"></script>
    <% else %>
    <% end %>

When I test it using text only, it works correctly

<% if post.copyright == true %>      
        Sample text here only applies to post where copyright==true
        <% else %>
        <% end %>

How can I get the script to only apply to certain posts?

解决方案

After your clarified in a comment

Yeah, including javascript will apply pagewide if nothing else is specified in the javascript.

You need to do something like:

<div class="post <%= 'copyright' if post.copyright%>">
  <img src="somesrc.jpg"></img>
</div>

and then apply the javascript only to the css selector 'div.copyright img'. Something along the lines of that. Depends on what the javascript supports or if you can change the source.

这篇关于为什么即使在此代码中,脚本也会影响我的 Rails 3 应用程序上的所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆