[英]How to append a script using javascript inside a div
I want to append a script generated in javascript inside a div created before in HTML, the problem is that only works if 我想在HTML之前创建的div内附加在javascript中生成的脚本,问题是仅在以下情况下有效
$("<script />", {
"src":"http://en.parkopedia.co.uk/js/embeds/mapView.js",
"data-location":"http://en.parkopedia.co.uk/parking/" + search_field2,
"data-options":"l=0&tc=0&zc=1&country=UK&ts[]=4&ts[]=3&ts[]=2",
"data-size":"650:400",
"id":"script_map",
"type":"text/javascript"
}).appendTo("body")
but if I change that for "$('#divId')" does not work, so the questions are: 但是如果我将其更改为“ $('#divId')”不起作用,那么问题是:
I've created a demo here : 我在这里创建了一个演示:
Update: The main aim is to update the data-location when you click on the button and send the text inside the box to the data location to search there and show the script map below the textbox. 更新:主要目的是在单击按钮时将数据位置更新,并将框内的文本发送到数据位置以在此处搜索并在文本框下方显示脚本映射。 Maybe it is another simple way to do it. 也许这是另一种简单的方法。
This will answer your first question. 这将回答您的第一个问题。
.appendTo("#map")
And for second question. 第二个问题。
if ($("#script_map").length > 0) {
$("#map").html("");
}
DEMO : 演示:
http://jsfiddle.net/kishoresahas/oosttuc0/4/ http://jsfiddle.net/kishoresahas/oosttuc0/4/
Code : 代码:
window.scripting = function () { if ($("#script_map").length > 0) { $("#map").html(""); } var src_fld = document.getElementById('txt_f').value; $("<script />", { "src": "//en.parkopedia.co.uk/js/embeds/mapView.js", "data-location": "//en.parkopedia.co.uk/parking/" + src_fld, "data-options": "l=0&tc=0&zc=1&country=UK&ts[]=4&ts[]=3&ts[]=2", "data-size": "650:400", "id": "script_map", "type": "text/javascript" }).appendTo("#map") }
<script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.js"></script> <script type="text/javascript" src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> <script type="text/javascript" src="//code.jquery.com/jquery.min.js"></script> <script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <div class="tab-pane carpark" id="main_view"> <input type="text" id="txt_f" name="txt_f"> <input type="submit" class="btn btn-default" id="btn" value="Submit" onclick="scripting(); return false;"> <div id="map" name="map"></div> <iframe style="border:none" width="100%" height="500px" id="iFrame2" name="iFrame2"></iframe> </div>
Run the Date input line 运行日期输入行
DateInput('creneau_2',true,'DD-MON-YYYY');
you could just try and do it all in jQuery rather than mixing javascript selectors. 您可以尝试在jQuery中完成所有操作,而不是混合使用javascript选择器。
$("#test").append("<script type='text/javascript'>DateInput('creneau_2',true,'DD-MON-YYYY');</script>");
Look this: 看看这个:
function scripting(){ var src_fld = document.getElementById('txt_f').value ; $("#main_view").find("script[id='test']").remove(); console.log("Size: " + $("#main_view").size()); $("<script id='test'/>", { "src":"http://en.parkopedia.co.uk/js/embeds/mapView.js", "data-location":"http://en.parkopedia.co.uk/parking/" + src_fld, "data-options":"l=0&tc=0&zc=1&country=UK&ts[]=4&ts[]=3&ts[]=2", "data-size":"650:400", "id":"script_map", "type":"text/javascript" }).appendTo($("#main_view")) console.log($("#main_view").html()) }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="tab-pane carpark" id="main_view"> <input type="text" id="txt_f" name="txt_f"> <input type="button" class="btn btn-default" id="btn" value="Submit" onclick="scripting(); return false;"> <div id="map" name="map"></div> <iframe style="border:none" width="100%" height="500px" id="iFrame2" name="iFrame2"></iframe> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.