【问题标题】:Looking for a javascript solution equivalent to below jQuery solution寻找相当于以下 jQuery 解决方案的 javascript 解决方案
【发布时间】:2013-12-16 06:22:51
【问题描述】:

我在页面上有几个按钮,我正在尝试动态更改颜色(背景)

我编写了一个 jQuery,它可以在所有浏览器上运行,并根据传递给函数的 CSS 参数更改背景颜色。

我正在寻找一种更改按钮颜色的 javascript 方法,该方法应该适用于所有浏览器,类似于下面的 jQuery 实现。

function apply_Color(iframe,CSSParams){

var buttonColor = CSSParams.buttonColor;
var buttonHoverBackgroundColor = CSSParams.buttonHoverBackgroundColor;

jQuery(iframe).contents().find('.search-button').css({
'background': buttonColor,
'background-image': '-moz-linear-gradient(top,' + buttonColor + ' 0%,' + buttonColor + ' 100%)', /* FF3.6+*/
'background-image': '-webkit-gradient(linear, left top, left bottom, color-stop(0%, ' + buttonColor + '), color-stop(100%, ' + buttonColor + '))', /* Chrome,Safari4+ */
'background-image': '-webkit-linear-gradient(top, ' + buttonColor + ' 0%, ' + buttonColor + ' 100%)', /* Chrome10+,Safari5.1+ */
'background-image': '-o-linear-gradient(top, ' + buttonColor + ' 0%, ' + buttonColor + ' 100%)', /* Opera 11.10+ */
'background-image': '-ms-linear-gradient(top, ' + buttonColor + ' 0%, ' + buttonColor + ' 100%)', /* IE10+ */
'background-image': 'linear-gradient(top, ' + buttonColor + ' 0%, ' + buttonColor + ' 100%)'/* W3C */
 });

jQuery(iframe).contents().find('.p-search-button').hover(function() {
 jQuery(this).css
 ({
 'background': buttonHoverBackgroundColor ,
 'background-image': '-moz-linear-gradient(top,' + buttonHoverBackgroundColor + ' 0%,' + buttonHoverBackgroundColor + ' 100%)', /* FF3.6+*/
 'background-image': '-webkit-gradient(linear, left top, left bottom, color-stop(0%, ' + buttonHoverBackgroundColor + '), color-stop(100%, ' + buttonHoverBackgroundColor + '))', /* Chrome,Safari4+ */
 'background-image': '-webkit-linear-gradient(top, ' + buttonHoverBackgroundColor + ' 0%, ' + buttonHoverBackgroundColor + ' 100%)', /* Chrome10+,Safari5.1+ */
 'background-image': '-o-linear-gradient(top, ' + buttonHoverBackgroundColor + ' 0%, ' + buttonHoverBackgroundColor + ' 100%)', /* Opera 11.10+ */
 'background-image': '-ms-linear-gradient(top, ' + buttonHoverBackgroundColor + ' 0%, ' + buttonHoverBackgroundColor + ' 100%)', /* IE10+ */
 'background-image': 'linear-gradient(top, ' + buttonHoverBackgroundColor + ' 0%, ' + buttonHoverBackgroundColor + ' 100%)'/* W3C */
 });
});
}

我尝试过javascript方式,没有运气;请帮忙。如果我的问题不清楚,请发表评论。我会编辑。我正在寻找可以为按钮(背景颜色)采用线性渐变的语法。

obj.style.backgroundColor["-webkit-linear-gradient"] = "blue";
obj.style.backgroundColor = "red";

【问题讨论】:

  • 为什么要删除 jQuery?
  • @Jerska 一个网站的要求,没有jQuery。项目需要的是没有 jQuery :(
  • 是否可以在生成 iframe 时直接将 CSS 添加到 iframe,而不是尝试使用 JavaScript 动态应用它?
  • @Jerska jquery 是一个相当重的库,它做了很多与项目相关的不必要的事情,以保持跨浏览器的适用性,并且在大多数项目中明确不需要。
  • @MT0 是的!只需将链接标签附加到 iframe 的头部元素

标签: javascript jquery css


【解决方案1】:

根据 MT0 的想法,你可以在 iframe 中添加一个样式表吗?

function applyCss(iframe,stylesheet_url){
    var link = document.createElement("link") 
    link.href = "style.css"; 
    link.rel = "stylesheet"; 
    link.type = "text/css"; 
    iframe.document.body.appendChild(cssLink);
}

[编辑] 对于线性渐变,您应该使用背景图像而不是背景颜色并将值设置为 css 函数

obj.style.backgroundImage = "-webkit-linear-gradient(left,blue,blue)";

整个脚本:

function apply_Color(iframe,CSSParams){

    var buttonColor = CSSParams.buttonColor;
    var buttonHoverBackgroundColor = CSSParams.buttonHoverBackgroundColor;

    var els = iframe.document.getElementsByClassName('.search-button');

    for(var i = 0; i<els.length; i++){
        els[i].style.background = buttonColor;
        els[i].style.backgroundImage = '-moz-linear-gradient(top,' + buttonColor + ' 0%,' + buttonColor + ' 100%)';
        els[i].style.backgroundImage = '-webkit-gradient(linear, left top, left bottom, color-stop(0%, ' + buttonColor + '), color-stop(100%, ' + buttonColor + '))';
        els[i].style.backgroundImage = '-webkit-linear-gradient(top, ' + buttonColor + ' 0%, ' + buttonColor + ' 100%)';
        els[i].style.backgroundImage = '-o-linear-gradient(top, ' + buttonColor + ' 0%, ' + buttonColor + ' 100%)';
        els[i].style.backgroundImage = '-ms-linear-gradient(top, ' + buttonColor + ' 0%, ' + buttonColor + ' 100%)';
        els[i].style.backgroundImage = 'linear-gradient(top, ' + buttonColor + ' 0%, ' + buttonColor + ' 100%)';
    }
    // same goes for hover
}

【讨论】:

  • 我做了这个,但是这个想法在项目中不被接受;寻找使用 javascript 语法应用所有浏览器渐变,如 jQuery。
  • 查看编辑。背景图像而不是背景颜色? obj.style.backgroundImage = "-webkit-linear-gradient(left,blue,blue)";
【解决方案2】:

凭直觉,我尝试了以下方法:

http://jsfiddle.net/FzU3V/2/

(特别是..)

target.style.background = "red";
target.style.background = "-moz-linear-gradient(top, blue, red)";
target.style.background = "-webkit-linear-gradient(top, green, red)";
target.style.background = "-o-linear-gradient(top, black, red)";

尝试每个浏览器 - 它们都显示适当的渐变。我的假设是,就像样式表一样,无效值会被忽略。我不小心炸毁了我的 IE 安装,但这确实适用于 chrome、opera(尽管它在 opera 18 中默认为 webkit 版本)和 FF。如果在我修复安装后这在 IE 上不起作用,我很乐意删除这个答案。

【讨论】:

    【解决方案3】:

    您的语法已关闭:

    obj.style.backgroundColor = 'blue'
    

    小提琴here

    【讨论】:

    • 两种语法都可以。 obj.style['background-color'] = obj.style.backgroundColor。我没有-1
    • 可以,但是 obj.style.backgroundColor["-webkit-linear-gradient"] 不起作用
    • Fiddle 在 FF 中不起作用。需要为 backgroundColor。
    • Fiddle 在 Firefox 中不起作用,但在 IE 中起作用。我该如何应用 ex:- -moz-linear-gradient(top,' + buttonHoverBackgroundColor + '0%,' + buttonHoverBackgroundColor + '100%)', /* FF3.6+*/ or webkit etc.,
    猜你喜欢
    • 2014-07-20
    • 2017-07-27
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-16
    相关资源
    最近更新 更多