[英]pass parameter to embedded href='javascript:function(“ + param + ”);
我正試圖在傳單彈出窗口中嵌入一個調用javascript函數。
我將showPopup()函數綁定到添加到地圖的每個功能。 這個想法是當用戶點擊該功能時,有一個標有“更多信息...”的href應打開側邊欄。
我正在嘗試將功能代碼傳遞給嵌入式'javascript:getInfoPanelData(此處為功能代碼,例如PIPE ),以了解我正在使用的功能。 但是,在Chrome調試器中運行此代碼會產生
未捕獲的ReferenceError:PIPE未定義於:1:18。
我還嘗試在參數周圍添加單引號,但這會產生一個SyntaxError:
意外的輸入結束(getInfoPanelData(調試器中的“red x”)。
我不確定我能不能做我需要做的事情,並希望有人可以指出我的錯誤或可能的替代方案。
/*** Code that builds the popup ***/ function showPopup(feature, urlString) { console.info("onEachFeature: " + feature.properties.Code + " | " + feature.properties.NAME); var pkVal = parseInt(feature.properties.ParkType, 10); var parkIcon = "nationalpark-40.png"; var retHtml = "<div id='popup' class='popup'>" + "<h3 align='center'><img src='icons/" + parkIcon + "'/>" + feature.properties.NAME + "</h3>" + "<p>State: " + feature.properties.State + " | parkCode: " + feature.properties.Code + " | parkType: " + pkVal + "</p>" + "<p>Home Page: " + "<a href='" + urlString + "' target='_blank'>" + urlString + "</p>" + "<p><a href='javascript:getInfoPanelData(" + feature.properties.Code + ");'> More Info...</a></p></div>"; console.info("HTML: " + retHtml); return retHtml; }
/*** Code that binds the popup to the feature ***/ /* Create our NPS Layer */ var npsCPs = new L.GeoJSON.AJAX("data/NPS_4326_CPs.json", { pointToLayer: function(feature, latlng) { return L.marker(latlng, { icon: npsIcon }); }, onEachFeature: function(feature, layer) { var urlStr = "https://www.nps.gov/" + feature.properties.Code + "/index.htm"; layer.bindPopup(showPopup(feature, urlStr)); } });
在代碼段中:
<a href='javascript:getInfoPanelData(" + feature.properties.Code + ");'>
你想引用feature.properties.Code
。 但是,當你在href
屬性中使用單引號時,你不能只在那里放一個單引號。 你也不能只在那里加雙引號,因為你的字符串使用雙引號。 你需要逃避你的報價,例如:
<a href='javascript:getInfoPanelData(\"" + feature.properties.Code + "\");'>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.