我已经尝试使用.load()并$.ajax获取一些需要附加到容器的HTML,但是当我将它附加到元素时,返回的HTML中的Javascript没有被执行.
使用.load():
$('#new_content').load(url+' #content > *',function() {
alert('returned');
});
Run Code Online (Sandbox Code Playgroud)
我也试过切换到$ .ajax调用.然后我从返回的HTML中提取脚本,之后我将它附加到容器中,但同样的问题,JS在这种情况下没有被执行或者甚至被附加,并且据我所知,尝试将一个附加<script>到DOM元素像这样不赞成?
使用$.ajax:
$.ajax({ url: url }).done(function(response) {
var source = $("<div>").html(response).find('#overview_script').html();
var content = $("<div>").html(response).find('#content').html();
$('#new_content').html(content);
$('<script>').appendTo('#new_content').text(source).html();
});
Run Code Online (Sandbox Code Playgroud)
理想情况下,我在附加HTML后在回调中运行此javascript,但变量被设置为从控制器返回的值.
总而言之,我试图获得一些HTML,其中包含需要在附加HTML后运行的JS,但我没有运气,.load()或者$.ajax()因为返回的HTML中的脚本在附加时消失,或者它根本不执行.
我刚刚在我的网站上实现了ShareThis的facebook分享按钮,我可以分享,但是,我想在模式(弹出窗口)中打开确认对话框,而不是每次都在新选项卡中打开.
我看了一下ShareThis的文档,在这里我说我只需要popup: 'true'在初始化时传递参数,我已经完成了,但它没有任何效果,只要按下按钮它就会在新选项卡中打开.
控制台中没有错误,共享功能正常,我错过了什么?
这是我的代码:
<script type="text/javascript" src="https://ws.sharethis.com/button/buttons.js"></script>
<script type="text/javascript">
//init
stLight.options({popup:'true', publisher: "abcdef-1234-1234-1234-1234abcdef1234", doNotHash: false, doNotCopy: false, hashAddressBar: false});
//create widget
stWidget.addEntry({"service":"facebook", "element":document.getElementById('fb_share_button'), "url":"http://www.site.com/link/to/share/" , "title":"Title", "type":"hcount" });
</script>
<span st_url="<!-- url is assigned dynamically -->" class='st_facebook_hcount' displaytext='Facebook' id="fb_share_button"></span>
Run Code Online (Sandbox Code Playgroud) 我试图实例化同一对象的多个实例.第一个实例化工作正常,但是当我尝试初始化另一个对象时,我收到此错误,
Uncaught TypeError: Object #<draw> has no method 'width'
这是小提琴,这是我的代码:
function halo() {
var width = 720, // default width
height = 80; // default height
function draw() {
// main code
console.log("MAIN");
}
draw.width = function(value) {
if (!arguments.length) return width;
width = value;
return draw;
};
draw.height = function(value) {
if (!arguments.length) return height;
height = value;
return draw;
};
return draw;
}
var halo = new halo();
halo.width(500);
var halo2 = new halo();
halo2.width(300);
Run Code Online (Sandbox Code Playgroud)
总之,我的目标是实例化同一"类"的多个实例.