【发布时间】:2017-09-15 04:33:23
【问题描述】:
在相关的question 中,有人建议我可以通过编辑元素“样式”标签来动态更新媒体查询。我知道可以使用 .css 方法使用 jquery 来完成,但显然样式标签本身需要更新。
我的最终目标是附加整个媒体查询,正如此处所述,我无法使用 .css 的更好方法。如果我使用 .css,那么屏幕上的类和媒体查询会发生变化。但是,我只想更改媒体查询。问题是我试图改变样式标签中的字体颜色,到目前为止还没有运气。
我感谢到目前为止的所有回复。
这是我迄今为止尝试过的:
function myFunction() {
$('.changeMe').append("<style type='text/css'>color:blue</>")
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="myFunction()">Click me to change text from black to blue</button>
<p class="changeMe">Change My Color To Blue by adding a style tag</p>
这是一个代码 sn-p,显示使用 .css 更改了我不想要的屏幕上的 div。
var canvas = document.getElementById("canvas1");
function draw_a() {
var context = canvas.getContext("2d");
// // LEVER
//plane
context.fillStyle = '#aaa';
context.fillRect (25, 90, 2500, 400);
}
function changeScale() {
var scale = .1;
console.log("scale:" + scale);
$('.myDivToPrint').css({
'-webkit-transform': 'scale(' + scale + ')',
'-moz-transform': 'scale(' + scale + ')',
'-ms-transform': 'scale(' + scale + ')',
'-o-transform': 'scale(' + scale + ')',
'transform': 'scale(' + scale + ')',
});
}
$(document).ready(function(){
draw_a();
});
div.sizePage {
color: #333;
}
canvas {
border: 1px dotted;
}
.printOnly {
display: none;
}
@media print {
html,
body {
height: 100%;
background-color: yellow;
}
.myDivToPrint {
background-color: yellow;
top: 0;
left: 0;
margin: 0;
}
.no-print,
.no-print * {
display: none !important;
}
.printOnly {
display: block;
}
}
@media print and (-ms-high-contrast: active) and (-ms-high-contrast: none) {
html,
body {
height: 100%;
background-color: pink;
}
.myDivToPrint {
background-color: yellow;
/*
-moz-transform: rotate(90deg) scale(0.3);
-ms-transform: rotate(90deg) scale(0.3);
-o-transform: rotate(90deg) scale(0.3);
-webkit-transform: rotate(90deg) scale(0.3);
transform: rotate(90deg) scale(0.3); /* Standard Property */
position: fixed;
top: 0;
left: 0;
margin: 0;
padding: 15px;
font-size: 14px;
line-height: 18px;
position: absolute;
display: flex;
align-items: center;
justify-content: center;
/*
-webkit-transform: rotate(90deg);
-moz-transform: rotate(90deg);
-o-transform: rotate(90deg);
-ms-transform: rotate(90deg);
transform: rotate(90deg);
*/
}
.no-print,
.no-print * {
display: none !important;
}
.printOnly {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="window.print();" class="no-print">Print Canvas</button>
<button onclick="changeScale();" class="no-print>">Change Scale (Want this to only change scale in media query.)</button>
<div class="myDivToPrint">
<canvas height="2500px" width="4000px" id="canvas1"></canvas>
</div>
这是一个代码 sn-p,我想添加我需要的媒体查询。
var canvas = document.getElementById("canvas1");
function draw_a() {
var context = canvas.getContext("2d");
// // LEVER
//plane
context.fillStyle = '#aaa';
context.fillRect (25, 90, 2500, 400);
}
function changeScale() {
var scale = .1;
console.log("scale:" + scale);
$('.myDivToPrint').append("<style type='text/css'>@media print and (-ms-high-contrast: active) and (-ms-high-contrast: none){"
+ "-webkit-transform: scale(" + scale + ") translate(100px,100px) rotate(-90deg),"
+ "-moz-transform: scale(" + scale + ") translate(100px,100px) rotate(-90deg),"
+ "-ms-transform: -transform: scale(" + scale + ") translate(100px,100px) rotate(-90deg),"
+ "-o-transform: scale(" + scale + ") translate(100px,100px) rotate(-90deg),"
+ "transform: scale(" + scale + ") translate(100px,100px) rotate(-90deg),"
+ "</style>");
}
$(document).ready(function(){
draw_a();
});
div.sizePage {
color: #333;
}
canvas {
border: 1px dotted;
}
.printOnly {
display: none;
}
@media print {
html,
body {
height: 100%;
background-color: yellow;
}
.myDivToPrint {
background-color: yellow;
top: 0;
left: 0;
margin: 0;
}
.no-print,
.no-print * {
display: none !important;
}
.printOnly {
display: block;
}
}
@media print and (-ms-high-contrast: active) and (-ms-high-contrast: none) {
html,
body {
height: 100%;
background-color: pink;
}
.myDivToPrint {
background-color: yellow;
position: fixed;
top: 0;
left: 0;
margin: 0;
padding: 15px;
font-size: 14px;
line-height: 18px;
position: absolute;
display: flex;
align-items: center;
justify-content: center;
}
.no-print,
.no-print * {
display: none !important;
}
.printOnly {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="window.print();" class="no-print">Print Canvas</button>
<button onclick="changeScale();" class="no-print>">Change Scale</button>
<div class="myDivToPrint">
<canvas height="2500px" width="4000px" id="canvas1"></canvas>
</div>
谢谢, 马特
【问题讨论】:
-
请阅读doc,因为它将解释动态应用jQuery css的正确方法。
-
所以更好的设计不是这样做并使用主题......
-
我留下了答案 OP,但我不是 100% 为什么要更新媒体查询而不是更改 jQuery 目标元素的样式。
-
可以做到,但是有很多比破解
style元素更好的方法 -
您说您必须编辑其自身的样式标签,这意味着内联样式。您没有使用内联样式。此外,想要在 DOM 中编辑实际的样式标签是……奇怪。为什么/您必须更改标签而不是直接应用 CSS 的理由是什么?
标签: javascript jquery html css