- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我很快就要发火了(提问者注意)
这是我正在使用的 SVG 图像:http://en.wikipedia.org/w/index.php?title=File:World98.svg&page=1
这是一个免费的公共(public)领域 - 用 SVG 制作的全局 map ,其中(几乎)每个国家都被各自的边界所包围。
先别去那里!您不必立即寻找它。
我还有件事要先告诉你。
这是图片的相关部分:
(编辑)西类牙是我在两个示例中试图操纵的国家/地区
<g
id="gESP">
<path
class="region"
d="M472.55,197.77 472.27,198.22 471.8,197.9 472.55,197.77Z"
id="polyF1S67P1" />
<path
class="region"
d="M479.45,196.82 479.35,197.75 478.75,197.9 478.33,197.55 478.67,196.8 479.45,196.82Z"
id="polyF1S67P2" />
<path
class="region"
d="M474.5,197.17 474.1,196.9 474.3,196.67 474.72,196.92 474.5,197.17Z"
id="polyF1S67P3" />
<path
class="region"
d="M476.85,196.17 475.95,197.22 475.27,196.3 477.45,195.62 476.85,196.17Z"
id="polyF1S67P4" />
<path
class="region"
d="M482.8,196.77 481.97,196.95 482.7,196.62 483.33,195.27 483.75,195.17 483.55,196.52 482.8,196.77Z"
id="polyF1S67P5" />
<path
class="region"
d="M472.7,195.88 472.22,195.12 472.42,194.9 473,195.22 472.7,195.88Z"
id="polyF1S67P6" />
<path
class="region"
d="M484.12,194.7 483.72,194.82 483.92,194.3 484.95,193.9 484.8,194.45 484.12,194.7Z"
id="polyF1S67P7" />
<path
class="region"
d="M526.65,167.6 526.12,167.47 526.65,167.6Z"
id="polyF1S67P8" />
<path
class="region"
d="M526.25,166.9 525.62,166.95 525.97,166.45 526.72,166.42 526.25,166.9Z"
id="polyF1S67P9" />
<path
class="region"
d="M530.97,164.47 531.85,164.75 530.77,165.85 529.72,165.17 528.82,165.1 530.3,164.15 531.12,164 530.97,164.47Z"
id="polyF1S67P10" />
<path
class="region"
d="M534.15,164.35 532.97,164.02 532.95,163.72 533.97,163.8 534.15,164.35Z"
id="polyF1S67P11" />
<path
class="region"
d="M500.85,153.45 502.62,154.02 506,153.77 509.67,154.4 512.23,154.12 515.75,154.65 517.25,154.42 518.32,154.9 518.17,155.42 518.62,155.27 520.6,156.12 523.67,156.45 524.35,156.02 525.97,156.42 526.38,157.15 529.6,157.4 530.47,157.05 531.43,157.55 531.02,157.62 531.25,158.5 530.57,159 528.02,160.32 525.1,160.95 524.22,161.62 524.62,162 523.9,162.2 521.32,165.22 521.67,166.5 522.8,167.35 520.8,168.57 519.97,170.1 520.1,170.57 518.55,170.67 517.67,171.15 516.15,172.95 515.42,172.7 514.5,173.02 510.1,173.02 509.25,173.6 507.88,173.82 506.6,174.95 505.45,174.5 504.5,173.25 504.97,172.47 504.47,172.7 503.17,171.45 501.65,171.72 501.4,170.77 502.9,168.92 502.5,168.95 501.82,168.17 502.8,166.52 501.3,164.85 502.7,164.7 503.07,163.85 502.7,163.42 503.32,162.95 503.02,161.02 504.9,159.45 504.05,159.25 503.85,158.5 499.6,158.88 499.27,157.97 497.65,158.55 497.55,158.05 498.1,157.57 497.75,157.55 497.8,156.67 497.15,156.7 497.42,156.12 496.57,155.62 496.75,155.07 497.57,154.65 499.02,154.47 499.3,153.95 500.85,153.45Z"
id="polyF1S67P12" />
</g>
我想做的,就是用 Javascript 把国家填成红色。
我已经试过了:
尝试#1 - 没有成功
function selectedCountryChanged(id) {
var tmp = $("#selectedCountry").find("option:selected");
var val = tmp.data('alpha');
console.log('val'); // shows ESP in console
for (var s in a.style) {
console.log(s); // shows correctly all style properties in console
}
var a = document.getElementById("g"+val);
a.style.fill = '#ffff00';
console.log(a.style.fill); // shows rgb(255, 0, 0) in console
}
尝试#2 - 没有成功
function selectedCountryChanged(id) {
var tmp = $("#selectedCountry").find("option:selected");
var val = tmp.data('alpha');
var a = document.getElementById("g"+val);
console.log(val); // shows ESP in console
for(var s in a.style) {
console.log(s); // shows all css-properties correctly
}
console.log(id + " " + val);
a.setAttribute("style", "fill: #ff0000");
console.log(a.style.getPropertyValue("fill")); // shows rgb(255, 0, 0) in console
}
即使所有的调试信息都显示一切正常,这该死的东西为什么不填充红色也一定是有原因的?如果我通过在 SVG 文件中 fork css 部分来手动执行此操作,它显示正常。
我只包含了代码中最相关的部分(恕我直言)。我会根据要求发布更多内容。
编辑:已使用 Google Chrome 和 Mozilla Firefox 进行测试。两者都失败了。
编辑#2:在这两种方法中,Firebug 和 Javascript 控制台(在 Chrome 中)都没有给出错误。
编辑:
@Bill 在他的评论中和@Simon Boudrias 在他的回答中都指出了问题所在。
主元素的很多子路径被白色填充,需要覆盖。解决方案最终是微不足道的。
解决方案
function selectedCountryChanged(id) {
// use the selection id to get alpha3-country identifier
var tmp = $("#selectedCountry").find("option:selected");
var val = tmp.data('alpha');
// Get the style - element defined in SVG
var a = document.getElementById("mapStyle");
// By assigning the * as class name, we quarantee
// that all the children are filled with red as well
var newFill = "#g"+val+" * { fill: red; }";
// If prevSelectedCountry is set, remove the red filling by
// setting it white
if (window.prevSelectedCountry != undefined) {
$(a).append(window.prevSelectedCountry);
}
// Add this country to prevSelectedCountry
window.prevSelectedCountry = "#g"+val+" * { fill: white; }";
// Fill this country with red
$(a).append(newFill);
}
最佳答案
当我查看 .svg 图像时,我看到大部分 <path>
元素有一个 fill
属性设置为白色。因此,您需要先覆盖或删除此属性。
当您选择一个组时 <g>
元素。因此,为了使填充属性或样式属性在其子项上起作用 <path>
您需要清除或更改自己的填充属性。
因此,您可以对此进行测试:(可能需要一些时间,具体取决于您使用的某些变量的值)
function selectedCountryChanged(id) {
var tmp = $("#selectedCountry").find("option:selected");
var val = tmp.data('alpha');
var paths = $("#g" + val).find('path');
paths.attr("fill", "#ff0000");
}
关于javascript - 为什么这个 Javascript - SVG CSS 操作不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12876164/
我有一个 html 格式的表单: 我需要得到 JavaScript在value input 字段执行,但只能通过表单的 submit .原因是页面是一个模板所以我不控制它(不能有
我管理的论坛是托管软件,因此我无法访问源代码,我只能向页面添加 JavaScript 来实现我需要完成的任务。 我正在尝试用超链接替换所有页面上某些文本关键字的第一个实例。我还根据国家/地区代码对这些
我正在使用 JS 打开新页面并将 HTML 代码写入其中,但是当我尝试使用 document.write() 在新页面中编写 JS 时功能不起作用。显然,一旦看到 ,主 JS 就会关闭。用于即将打开的
提问不是为了解决问题,提问是为了更好地理解系统 专家!我知道每当你将 javascript 代码输入 javascript 引擎时,它会立即由 javascript 引擎执行。由于没有看过Engi
我在一个文件夹中有两个 javascript 文件。我想将一个变量的 javascript 文件传递到另一个。我应该使用什么程序? 最佳答案 window.postMessage用于跨文档消息。使
我有一个练习,我需要输入两个输入并检查它们是否都等于一个。 如果是 console.log 正则 console.log false 我试过这样的事情: function isPositive(fir
我正在做一个Web应用程序,计划允许其他网站(客户端)在其页面上嵌入以下javascript: 我的网络应用程序位于 http://example.org 。 我不能假设客户端网站的页面有 JQue
目前我正在使用三个外部 JS 文件。 我喜欢将所有三个 JS 文件合而为一。 尽一切可能。我创建 aio.js 并在 aio.js 中 src="https://code.jquery.com/
我有例如像这样的数组: var myArray = []; var item1 = { start: '08:00', end: '09:30' } var item2 = {
所以我正在制作一个 Chrome 扩展,它使用我制作的一些 TamperMonkey 脚本。我想要一个“主”javascript 文件,您可以在其中包含并执行其他脚本。我很擅长使用以下行将其他 jav
我有 A、B html 和 A、B javascript 文件。 并且,如何将 A JavaScript 中使用的全局变量直接移动到 B JavaScript 中? 示例 JavaScript) va
我需要将以下整个代码放入名为 activate.js 的 JavaScript 中。你能告诉我怎么做吗? var int = new int({ seconds: 30, mark
我已经为我的 .net Web 应用程序创建了母版页 EXAMPLE1.Master。他们的 I 将值存储在 JavaScript 变量中。我想在另一个 JS 文件中检索该变量。 示例1.大师:-
是否有任何库可以用来转换这样的代码: function () { var a = 1; } 像这样的代码: function () { var a = 1; } 在我的浏览器中。因为我在 Gi
我收到语法缺失 ) 错误 $(document).ready(function changeText() { var p = document.getElementById('bidp
我正在制作进度条。它有一个标签。我想调整某个脚本完成的标签。在找到可能的解决方案的一些答案后,我想出了以下脚本。第一个启动并按预期工作。然而,第二个却没有。它出什么问题了?代码如下: HTML:
这里有一个很简单的问题,我简单的头脑无法回答:为什么我在外部库中加载时,下面的匿名和onload函数没有运行?我错过了一些非常非常基本的东西。 Library.js 只有一行:console.log(
我知道 javascript 是一种客户端语言,但如果实际代码中嵌入的 javascript 代码以某种方式与在控制台上运行的代码不同,我会尝试找到答案。让我用一个例子来解释它: 我想创建一个像 Mi
我如何将这个内联 javascript 更改为 Unobtrusive JavaScript? 谢谢! 感谢您的回答,但它不起作用。我的代码是: PHP js文件 document.getElem
我正在寻找将简单的 JavaScript 对象“转储”到动态生成的 JavaScript 源代码中的最优雅的方法。 目的:假设我们有 node.js 服务器生成 HTML。我们在服务器端有一个对象x。
我是一名优秀的程序员,十分优秀!