简体   繁体   English

将参数传递给嵌入式href ='javascript:function(“+ param +”);

[英]pass parameter to embedded href='javascript:function(“ + param + ”);

I'm trying to embed a call to a javascript function in a leaflet popup. 我正试图在传单弹出窗口中嵌入一个调用javascript函数。

I'm binding the showPopup() function to each feature added to a map. 我将showPopup()函数绑定到添加到地图的每个功能。 The idea is when the user clicks on the feature, there is an href labeled "More info..." which should open a sidebar. 这个想法是当用户点击该功能时,有一个标有“更多信息...”的href应打开侧边栏。

I'm attempting to pass in the features Code to the embedded 'javascript:getInfoPanelData(feature code here, say PIPE as example) to know which feature I'm working with. 我正在尝试将功能代码传递给嵌入式'javascript:getInfoPanelData(此处为功能代码,例如PIPE ),以了解我正在使用的功能。 However, running this code in Chrome debugger yields 但是,在Chrome调试器中运行此代码会产生

Uncaught ReferenceError: PIPE is not defined at :1:18. 未捕获的ReferenceError:PIPE未定义于:1:18。

I've also tried to add single quotes around the parameter, but this yields a SyntaxError: 我还尝试在参数周围添加单引号,但这会产生一个SyntaxError:

Unexpected end of input (getInfoPanelData("red x" in debugger. 意外的输入结束(getInfoPanelData(调试器中的“red x”)。

I'm not sure I can actually do what I need to do and am hoping someone can either point out my mistake or possible alternatives. 我不确定我能不能做我需要做的事情,并希望有人可以指出我的错误或可能的替代方案。

 /*** 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)); } }); 

in the code snippet: 在代码段中:

<a href='javascript:getInfoPanelData(" + feature.properties.Code + ");'>

you do want to quote the feature.properties.Code . 想引用feature.properties.Code However, you can't just put a singe-quote in there as you're using single-quotes for the href property. 但是,当你在href属性中使用单引号时,你不能只在那里放一个单引号。 You also can't just put double-quotes in there because you're using double-quotes for your string. 你也不能只在那里加双引号,因为你的字符串使用双引号。 You need to escape your quotes, for example: 你需要逃避你的报价,例如:

<a href='javascript:getInfoPanelData(\"" + feature.properties.Code + "\");'>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM