我想使用javascript修改一个标签。我有一个文本框,介绍一个邮政编码和一个按钮,找到该位置附近的停车场。 HTML标记如下:


 < script src =“http://en.parkopedia.co.uk/js/embeds/mapView.js “data-Location =”http://en.parkopedia.co.uk/parking/ST84JF/“data-options =”l = 0& tc = 0& zc = 1& country = UK& ts [] = 4& ts [] = 3& ts [] = 2“data-size =”750:400“type =”text / javascript“>< / script>

&#xA ;
 我有两个问题:


代码更新:


 function find(){
 $( “脚本[ID = script_map]”)除去()
 var search = document.getElementById('box_txt')。value;
 $(“< script />”,{
“src”:“http://en.parkopedia.co.uk/js/embeds/mapView.js”,
“data-location “:”http://en.parkopedia.co.uk/parking/“+ search,
”data-options“:”l = 0& tc = 0& zc = 1& country = UK& ts [ ] = 4& ts [] = 3& ts [] = 2“,
”data-size“:”750:400“,
”id“:”script_map“,
”输入“:”text / javascript“
})。appendTo(”#divId“)
}



答案 0 :(得分:3)
尝试修改data-location
元素的script
,然后在script
元素的body
事件中将click
元素追加到button
。也可以使用$.getScript()
从外部来源
script
$("button").click(function() {
var val = $("input").val();
// Can I activate the script only when the button is clicked
$("<script />", {
"src":"http://en.parkopedia.co.uk/js/embeds/mapView.js",
// Can I modify the data-location to add the text write inside the textbox
// to search in that location?
"data-location":"http://en.parkopedia.co.uk/parking/ST84JF/" + val,
"data-options":"l=0&tc=0&zc=1&country=UK&ts[]=4&ts[]=3&ts[]=2",
"data-size":"750:400",
"type":"text/javascript"
}).appendTo("body")
})
答案 1 :(得分:0)
是的,数据位置只是一个自定义属性,我相信只需使用
document.getElementById("SCRIPTTAGID").setAttribute("data-location", "newValue");
至于按下按钮时激活要执行的脚本,您需要将其包含在函数中,然后在按钮的onClick事件中调用该函数。