javascript - Jquery Javascript 仅适用于 chrome 和 firefox,但不适用于 IE

标签 javascript jquery google-chrome internet-explorer firefox

我有一个包含 CSS 和 Jquery 的 HTML 文件。当我在 chrome 和 Firefox 上测试它时,它工作正常。但是我在IE上测试的时候,只有CSS部分起作用,JavaScript中的点击功能根本不起作用。

$(document).ready(function(){

    var prev;       
    $('svg polygon').click(function(){
        var current = this;
        this.classList.add('mouseclick');
        $('#suburb').val(current.id);


        if((typeof prev != 'undefined') && (current != prev)){
            prev.classList.remove("mouseclick");
        }


        prev=current;
    });

    $("#forwardbutton").click(function(){
        $("#ctlform").submit();
    });

});
svg polygon{
    fill:none;
    stroke: white;
    stroke-width:1px;    
}

svg polygon:hover{
    fill: rgba(255, 255, 0, 0.3);
}

svg polygon.mouseclick{
    fill: rgba(255, 0, 4, 0.3);      !important;
    stroke-width: 1px;
    stroke: rgb(255, 0, 4);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<form role="form" id="ctlform">
 <div>
<svg height="630" width="840">
    <image width="840" height="630" xlink:href="http://choicestudies.com/img/IGA/base.png" />
    <polygon points="536,379 535,377 536,374 539,368 541,367 541,363 539,363 540,359 546,362 557,362 559,358 559,359 565,358 568,359 567,363 563,368 564,382 562,386 558,386 554,390 554,392 552,392 551,395 547,397 547,399 545,398 548,392 548,386 546,385 537,386 536,379" id="Ashfield"> 
        <title>Ashfield</title>
    </polygon>
    <polygon points="437,360 438,358, 437,345 441,337 441,331 439,329 439,326 441,320 452,318 455,317 457,314 468,312 472,304 483,304 483,306 485,307 488,307 493,302 499,304 493,311 493,315 496,316 497,318 496,321 497,324 501,325 501,334 496,339 490,342 486,340 482,341 480,347 480,349 482,349 479,350 479,353 486,363 484,364 482,371 467,377 451,378 447,373 445,373 443,370 441,370 439,367 436,366 437,360" id="Auburn"/>


    <polygon points="403,430 405,428 405,425 399,418 399,405 402,405 404,403 404,400 397,394 397,390 393,389 393,383 394,382 399,383 402,372 405,369 406,365 408,364 413,353 421,360 423,360 436,370 438,370 438,372 443,374 451,381 468,380 478,375 480,375 481,380 485,380 486,378 489,377 485,402 489,404 494,404 488,406 486,413 478,422 468,425 467,430 461,431 461,443 462,449 464,450 464,469 467,472 468,476 462,477 456,481 443,483 441,487 438,489 438,491 430,490 430,486 426,486 422,490 420,486 424,483 424,481 417,475 417,473 414,470 406,469 406,464 408,463 407,457 404,455 399,455 395,446 391,445 394,442 393,432 395,432 397,429 399,431 403,430" id="Bankstown"/>

</svg>
</div>

<div>
     <p>Suburb Name:</p><br />
     <input type="text" id="suburb" name="sub"/>

</div>
</form>

解决方案:
classList.add() 在 IE 中不兼容。
所以将其更改为 .attr("class", "classname") 将解决问题。

最佳答案

作为 Shan suggested ,您的问题是您对 classList 的使用;然而,jQuery cannot add a class to an SVG。不过,您可以通过在 class 上设置 <svg> 属性来解决这个问题。

您可以通过运行下面的代码片段来查看实际效果。

$(document).ready(function(){
	
	var prev;       
	$('svg polygon').click(function(){
		var current = this;
		$(this).attr("class", "mouseclick");
		$('#suburb').val(current.id);

		if((typeof prev != 'undefined') && (current != prev)){
			$(prev).attr("class", "");
		}
				   
		prev=current;
	});

	$("#forwardbutton").click(function(){
		document.getElementById("ctlform").submit();
	});

});
svg polygon{
	fill:none;
	stroke: white;
	stroke-width:1px;    
}

svg polygon:hover{
	fill: rgba(255, 255, 0, 0.3);
}

svg polygon.mouseclick{
	fill: rgba(255, 0, 4, 0.3); !important;
	stroke-width: 1px;
	stroke: rgb(255, 0, 4);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<form role="form" id="ctlform">
 <div>
<svg height="630" width="840">
<image width="840" height="630" xlink:href="http://choicestudies.com/img/IGA/base.png" />
<polygon points="536,379 535,377 536,374 539,368 541,367 541,363 539,363 540,359 546,362 557,362 559,358 559,359 565,358 568,359 567,363 563,368 564,382 562,386 558,386 554,390 554,392 552,392 551,395 547,397 547,399 545,398 548,392 548,386 546,385 537,386 536,379" id="Ashfield"> 
    <title>Ashfield</title>
</polygon>
<polygon points="437,360 438,358, 437,345 441,337 441,331 439,329 439,326 441,320 452,318 455,317 457,314 468,312 472,304 483,304 483,306 485,307 488,307 493,302 499,304 493,311 493,315 496,316 497,318 496,321 497,324 501,325 501,334 496,339 490,342 486,340 482,341 480,347 480,349 482,349 479,350 479,353 486,363 484,364 482,371 467,377 451,378 447,373 445,373 443,370 441,370 439,367 436,366 437,360" id="Auburn"/>


<polygon points="403,430 405,428 405,425 399,418 399,405 402,405 404,403 404,400 397,394 397,390 393,389 393,383 394,382 399,383 402,372 405,369 406,365 408,364 413,353 421,360 423,360 436,370 438,370 438,372 443,374 451,381 468,380 478,375 480,375 481,380 485,380 486,378 489,377 485,402 489,404 494,404 488,406 486,413 478,422 468,425 467,430 461,431 461,443 462,449 464,450 464,469 467,472 468,476 462,477 456,481 443,483 441,487 438,489 438,491 430,490 430,486 426,486 422,490 420,486 424,483 424,481 417,475 417,473 414,470 406,469 406,464 408,463 407,457 404,455 399,455 395,446 391,445 394,442 393,432 395,432 397,429 399,431 403,430" id="Bankstown"/>

</svg>
</div>

<div>
 <p>Suburb Name:</p><br />
 <input type="text" id="suburb" name="sub"/>

</div>
</form>

关于javascript - Jquery Javascript 仅适用于 chrome 和 firefox,但不适用于 IE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28057255/

相关文章:

javascript - Chrome 扩展代码 vs 内容脚本 vs 注入(inject)脚本

javascript - 获取网页上使用 TAB 按钮选择的项目的标题

javascript - 嵌套 'for' 循环创建表

javascript - 使用 jquery 为 html 元素文本的每个字符设置动画?

javascript - 使用 chrome.storage.get 获取变量

php - 意外的 token 非法多行字符串 javascript+ php

javascript - npm 多个入口点

javascript - 获取开大括号的字符码

javascript - 如何将多个属性键作为参数传递给使用括号表示法的函数?

javascript - ng 包括不能在 chrome 浏览器中工作