Javascript 如何在javascript文件中包含css

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11356502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:36:52  来源:igfitidea点击:

How to include css inside a javascript file

javascripthtml

提问by Praveen Singh

Am newbie to to html, css and javascript. Currently am working on javascript to create piechart. my doubt is how can i include a css hover function within a function. CAn anyone help me to include css hover inside a function in the javascript included here. Thanks in advance

我是 html、css 和 javascript 的新手。目前正在使用 javascript 来创建饼图。我的疑问是如何在函数中包含 css 悬停功能。任何人都可以帮助我在此处包含的 javascript 中的函数内包含 css 悬停。提前致谢

<script type="text/javascript">
    function PieChart(id, o) {
        this.includeLabels = true;
        if (o.includeLabels == undefined) {
            this.includeLabels = false;
        }
        else {
            this.includeLabels = o.includeLabels;
        }
        this.data = o.data ? o.data : [30, 70, 45, 65, 20, 130]; 
        this.labels = o.labels ? o.labels : ["First", "Second", "Third", "Fourth", "Fifth", "Sixth"];
        this.colors = o.colors ? o.colors : [
                ["#bbddb3", "#1d8e04"], // green
                ["#e2f5b4", "#9edd08"], // yellow green
                ["#fdfbb4", "#faf406"], // yellow
                ["#fbd4b7", "#f2700f"], // orange
                ["#f8bdb4", "#ea2507"], // red
                ["#e2bcbd", "#9e2126"]  // purple
            ];

        this.canvas = document.getElementById(id);
    }

    PieChart.prototype = {

        select: function(segment) {
            var self = this;
            var context = this.canvas.getContext("2d");
            this.drawSegment(this.canvas, context, segment, this.data[segment], true, this.includeLabels);
        },
        draw: function() {

            var self = this;
            var context = this.canvas.getContext("2d");
            for (var i = 0; i < this.data.length; i++) {
            this.drawSegment(this.canvas, context, i, this.data[i], true, this.includeLabels);
            }
        },

        drawSegment: function(canvas, context, i, size, isSelected, includeLabels) {
            var self = this;
            context.save();
            var centerX = Math.floor(canvas.width / 2);
            var centerY = Math.floor(canvas.height / 2);
            radius = Math.floor(canvas.width / 2);

            var startingAngle = self.degreesToRadians(self.sumTo(self.data, i));
            var arcSize = self.degreesToRadians(size);
            var endingAngle = startingAngle + arcSize;

            context.beginPath();
            context.moveTo(centerX, centerY);
            context.arc(centerX, centerY, radius, startingAngle, endingAngle, false);
            context.closePath();

            isSelected ? 
                context.fillStyle = self.colors[i][1] :
                context.fillStyle = self.colors[i][0];

            context.fill();
            context.restore();

            if (includeLabels && (self.labels.length > i)) {
                self.drawSegmentLabel(canvas, context, i, isSelected);

            }
        },

        drawSegmentLabel: function(canvas, context, i, isSelected) {
            var self = this;
            context.save();
            var x = Math.floor(canvas.width / 2);
            var y = Math.floor(canvas.height / 2);
            var angle = self.degreesToRadians(self.sumTo(self.data, i));

            context.translate(x, y);
            context.rotate(angle);
            context.textAlign = 'center';
            var fontSize = Math.floor(canvas.height / 25);
            context.font = fontSize + "pt Helvetica";

            var dx = Math.floor(canvas.width * 0.5) - 100;
            var dy = Math.floor(canvas.height * 0.05);
            context.fillText(self.labels[i], dx, dy+dy);
            alert(self.labels[i]);
            context.restore();
        },

        drawLabel: function(i) {
            var self = this;
            var context = this.canvas.getContext("2d");
            var size = self.data[i];

            self.drawSegmentLabel(this.canvas, context, i, size, true);

        },

        degreesToRadians: function(degrees) {
        return (degrees * Math.PI)/180;
        },

        sumTo: function(a, i) {
            var sum = 0;
            for (var j = 0; j < i; j++) {
                sum += a[j];
            }
            return sum;
        }


    }
</script>

回答by jintian

  1. create style element and write some style rules.
    var styleEl = document.createElement('style');
    styleEl.innerHTML = 'body{color:blue; ... other styles}';
    document.head.appendChild(styleEl);
    
  2. set cssText, new rules will rewrite olds;
    document.body.style.cssText = 'color:#abcdef;';
  3. set style dierectly
    document.body.style.color = 'black';
  1. 创建样式元素并编写一些样式规则。
    var styleEl = document.createElement('style');
    styleEl.innerHTML = 'body{color:blue; ... other styles}';
    document.head.appendChild(styleEl);
    
  2. 设置cssText,新规则会改写旧规则;
    document.body.style.cssText = 'color:#abcdef;';
  3. 直接设置样式
    document.body.style.color = 'black';

there may be some other tricks.

可能还有一些其他的技巧。

回答by Maurice

I would recommend you start with jQuery as it is much easier to use than plain JavaScript and DOM manipulation.

我建议你从 jQuery 开始,因为它比普通的 JavaScript 和 DOM 操作更容易使用。

Take a look at the hoverevent and the css()function.

看一下悬停事件和css()函数。

回答by Abhijat

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){

$("p").hover(function(){
    $("p").css("color","red");
});
});
</script>
</head>

<body>
<p>This is a paragraph.</p>
</body>
</html>