English 中文(简体)
<object>由从$(document)调用的函数创建。ready未呈现
原标题:<object> created by function called from $(document).ready not rendered

在我整理的样本中,我需要:

  1. Use a jquery POST inside $(document).ready to grab a "ticket" I use later
  2. On success of the post, call another function ("AddTicketAndRender()") and pass in that ticket
  3. In AddTicketAndRender(), replace a placeholder value in an HTML template with the ticket that was passed in. The HTML template defines an object I need to render.
  4. 将HTML模板添加到body并呈现:

    function addTicketAndRender(incomingTicket){
    
        //For now, just touch the spinner, don t worry about the ticket.
        var template = $( #tableauTemplate ).html(),
            filledTemplate = template.replace( {placeholder} , yes );
    
    
        $( body ).append( filledTemplate );}
    

我在jsfidle中有这样的工作:

http://jsfiddle.net/vm4bG/4/一

然而,当我将HTML和JavaScript组合成一个htm文件时,我想要的可视化效果并没有在Chrome、IE或Firefox中呈现。

以下是HTM中无法正常工作的完整源代码。有人能看到明显错误的东西吗?我的标记&;脚本在下面和/或这里:http://tableau.russellchristopher.org:81/rfc1.htm一

<html>
<head>
<script type="text/javascript" src="http://public.tableausoftware.com/javascripts/api/viz_v1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
</head>

<!-- template code follows -->
<script type="text/template" id="tableauTemplate">

<div class="tableauPlaceholder" id="tableauPlaceholder" style="width:654px; height:1469px;background-image: url( http://tableau.russellchristopher.org:81/Background.gif ); top: 0px; left: 0px; width: 100%; margin-left: 76px;">
    <object class="tableauViz" width="654" height="1469">
        <param name="host_url" value="http%3A%2F%2Fpublic.tableausoftware.com%2F"/>
        <param name="site_root" value="" />
        <param name="name" value="AnalyticsIncJavaScript&#47;AnalyticsInc" />
        <param name="tabs" value="no" />
        <param name="toolbar" value="yes" />
        <param name="static_image" value="tableau.russellchristopher.org:81/Background.gif"/>
        <param name="animate_transition" value="yes" />
        <param name="display_static_image" value="yes" />
        <param name="display_spinner" value="{placeholder}" id="display_spinner" />
        <param name="display_overlay" value="yes" />
        <param name="display_count" value="yes" />
    </object>
</div>
</script>
<!-- end of template -->

<body>
<script>
function addTicketAndRender(incomingTicket){

// grab tableau template code and replace ticket placeholder with incomingTicket from $.post
console.log("Add and Render");

    //For now, just touch the spinner, don t worry about the ticket.
    var template = $( #tableauTemplate ).html(),
        filledTemplate = template.replace( {placeholder} , no );


    $( body ).append( filledTemplate );
    console.log(incomingTicket);
    console.log("Appended.");

}

$(document).ready(function() {
    console.log("ready");
    var trustedURL = "http://tableau.russellchristopher.org/trusted",
        userName = "foo",
        serverURL = "http://tableau.russellchristopher.org/";


    $.post(trustedURL, {
       username: userName,
        server: serverURL,
        client_ip: "",
        target_site: ""
    }, function(response) {
        addTicketAndRender(response);
    });


});

</script>


</body>

</html>      

在success函数中调用console.log会记录正确的信息,所以我知道我正在到达需要的地方,但对象似乎没有做它需要做的事情。

最佳回答

仅供参考,你的tableau.russellchristopher.org链接不起作用。我也不知道你怎么能在jsfidle中使用它——我尝试时遇到了跨源错误。

  1. 一个明显的问题是,包含模板的脚本元素位于</头部><;身体>body里面。

  2. 以下是我认为可能发生的情况:当DOMContentLoadedload触发时,TableauJavaScriptneneneba API似乎设置为处理对象。tableauViz元素。您正在插入<;对象>在异步请求的回调中标记到文档中。因此,我认为加载事件正在激发,Tableau API正在您的<code><;对象>标记被插入到文档中。

    也许为这些事件注册自己的监听器,并调用console.log(),看看它们是否在$.post

    不幸的是,执行初始化的createVizesAndStartLoading()方法(例如从文档中检索object.tableauVizwindow.tableau.createViz()来添加元素,但不幸的是createVizesAndStartLoading()会进行一些预处理(例如设置width/height

Retrieve template synchronously

试试这个,而不是你的$.post()

$.ajax( {

  url : trustedURL,

  data : {

    username : userName,

    server : serverURL,

    client_ip : "",

    target_site : ""

  },

  async : false

} ).done( addTicketAndRender );
问题回答

从所展示的大量代码中很难判断出你到底遇到了什么,所以一般原则是:

为了找到DOM元素并对其进行操作,该元素必须存在。例如,这段代码将失败:

<script>$("#target").html("Updated");</script>
<p id="target">Not updated</p>

实例|来源

目标不会更新。此代码将工作:

<p id="target">Not updated</p>
<script>$("#target").html("Updated");</script>

实例|来源

唯一的区别是script标签位于目标之后,因此当您尝试对其进行操作时,您知道目标存在于DOM中。(这是可靠的;请参阅谷歌开发团队对此有何看法,以及YUI指南。)

库传统上包含“DOM就绪”事件,因为内置的事件,即窗口对象的loadevent,直到非常晚才触发(一旦加载了所有外部引用,包括所有图像)。但人们想早点做,即使是在图像还在加载的时候。“ready”样式的事件允许脚本代码可以放置在标记中的任何位置(出于某种原因,人们喜欢把它放在head中),但如果它使用“ready“事件,它知道在处理完所有标记之前,它实际上不会被调用。

因此,结果是:要么使用ready事件,要么(更好地)将依赖于元素的代码移动到标记中这些元素之后的(通常最好在关闭





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!