我正在創建一個交互式地圖,並在一個道路塊的位。我需要發生的是基於SVG img中單擊的元素,工具提示窗口應顯示關於特定標記點的信息。綁定點擊事件以內聯SVG元素JS
現在,雖然我甚至無法得到任何標記來響應點擊事件。以下是我正在使用的代碼。一旦點擊事件實際上響應,我將關閉並運行。我只是堅持爲什麼這個點擊事件沒有響應(結果令人相當沮喪)。
我已閱讀了此SO帖子,希望能夠理解爲什麼點擊響應不會發生:Include SVG files with HTML, and still be able to apply styles to them?以及其他幾種不適用於此場景的解決方案。 SVG正在內嵌,由CSS設置的懸停事件正在發生,這只是未發生的單擊事件。任何幫助將不勝感激!
HTML:
<!DOCTYPE HTML>
<html>
<head>
<title>TITLE</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<link rel="stylesheet" href="{$url}/css/style.css">
<link rel="shortcut icon" href="{$url}/favicon.ico" type="image/x-icon">
<link rel="icon" href="{$url}/favicon.ico" type="image/x-icon">
</head>
<body>
<img src="{$url}/images/3d-map.svg" id="3d-map" class="svg">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script src="{$url}/js/scripts.js"></script>
</body>
</html>
的jQuery:
$(document).ready(function(){
/*
* Replace all SVG images with inline SVG
*/
$('img.svg').each(function(){
var $img = $(this);
var imgID = $img.attr('id');
var imgClass = $img.attr('class');
var imgURL = $img.attr('src');
$.get(imgURL, function(data) {
// Get the SVG tag, ignore the rest
var $svg = $(data).find('svg');
// Add replaced image's ID to the new SVG
if(typeof imgID !== 'undefined') {
$svg = $svg.attr('id', imgID);
}
// Add replaced image's classes to the new SVG
if(typeof imgClass !== 'undefined') {
$svg = $svg.attr('class', imgClass+' replaced-svg');
}
// Remove any invalid XML tags as per http://validator.w3.org
$svg = $svg.removeAttr('xmlns:a');
// Replace image with new SVG
$img.replaceWith($svg);
}, 'xml');
});
});
$(window).load(function(){
$(".exchange-marker").bind('click', function(e){
console.log('clicked');
console.log($(this).attr('id'));
});
});
CSS:
.exchange-marker:hover{
stroke: #fabf23;
stroke-width: 3;
transition: all 0.3s;
}
SVG(截斷簡潔):
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1400" height="1200" viewBox="0 0 1400 1200">
<g id="markers" fill="#2876BC">
<ellipse id="exchange-1" class="exchange-marker" cx="963.8" cy="853.7" rx="14.9" ry="4.8"/>
<ellipse id="exchange-2" class="exchange-marker" cx="929.3" cy="833.1" rx="14.9" ry="4.8"/>
</g>
</svg>
你好Tom,通常情況下,如果直截了當顯而易見的方法是行不通的,有時你必須手動添加複雜的「控制」層,所以你肯定知道它應該工作。我的意思是在整個SVG區域添加圖層蒙版。然後用數學來確定指針與'top'和'svg'下面的mask相關的位置。如果數學運算結果爲真,則觸發所需的事件。正如我所說,方式更臃腫和複雜,但最終的結果是有保證的。如果您向我們提供沙盒玩法,我可以更詳細地向您展示我的意思。 –
謝謝亞歷山大,我相信我明白你指的是什麼。有點類似於我們以前在HTML 4.01時期用來做圖像映射的方式,是否正確?我認爲這是朝着正確方向的一個很好的推動。如果在接下來的五分鐘內我沒有感覺到事情的發展,我會建立一個jsfiddle。再次感謝! – TomJ
是的,我相信那是當時使用的。由於我們正在討論解決任務的不同技術,您是否考慮過'