为什么我的HTML代码在Javascript代码之前运行

时间:2018-03-01 02:39:42

标签: javascript html jsf

我有多个帖子要在单页中显示。我的Java代码动态生成html,并为每个div分配一个数字。

<div onclick=\"postClicked(" + i + ");\" style=\"cursor: pointer;\">

当我点击div时,我想去ReadPost.xhtml页面并显示有关点击帖子的详细信息。

function postClicked(id) {  
  localStorage.setItem( 'objectToPass', id );
  location.href = 'ReadPost.xhtml';
}

在ReadPost页面中,我有以下代码:

<h:inputHidden id="postInput" value="#{postManager.clickedPostId};"/> 
 <script>                    
   var myData = localStorage['objectToPass'];
   document.getElementById("postInput").value = myData;
 </script>          

<h:outputText value="#{postManager.displaySinglePost()}" escape="false" />

我想用localStorage中的数据设置隐藏的输入值。这样当displaySinglePost()方法运行时,clickedPostId将会更新,我可以显示具有正确帖子ID的页面。但是,postManager.displaySinglePost()在脚本之前运行。如何在调用postManager.displaySinglePost()方法之前运行脚本?

1 个答案:

答案 0 :(得分:0)

您可能会在dom中的按钮之前注入javascript。要解决此问题,请在事件中包装javascript以确保加载dom:

CREATE TABLE EQUIPMENT
(
    /*Primary key*/
    EQUIPMENT_ID         VARCHAR(30),

    PRIMARY KEY (EQUIPMENT_ID)
);

CREATE TABLE PIPING
(
    /*Primary key*/
    EQUIPMENT_ID         VARCHAR(30), 

    /*Generic Fields*/
    STATUS               VARCHAR(1),
    INSPECTION_STATUS    VARCHAR(1),
    REMEDIATION_STATUS   VARCHAR(1),

    /*Specific properties for piping equipment*/
    OUTSIDE_DIAMETER     FLOAT,
    WALL_THICKNESS       FLOAT,

    /*About another additional 50 specific fields*/

    PRIMARY KEY (EQUIPMENT_ID),
    FOREIGN KEY (EQUIPMENT_ID) REFERENCES EQUIPMENT(EQUIPMENT_ID)
);

CREATE TABLE VESSELS
(
    /*Primary key*/
    EQUIPMENT_ID         VARCHAR(30), 

    /*Generic Fields*/
    STATUS               VARCHAR(1),
    INSPECTION_STATUS    VARCHAR(1),
    REMEDIATION_STATUS   VARCHAR(1),

    /*Specific properties for vessel equipment*/
    DISHED_END_THICKNESS FLOAT,
    DESIGN_CODE          VARCHAR(25),

    /*About another additional 50 specific fields*/

    PRIMARY KEY (EQUIPMENT_ID),
    FOREIGN KEY (EQUIPMENT_ID) REFERENCES EQUIPMENT(EQUIPMENT_ID)

);

CREATE TABLE TANKS
(
    /*Primary key*/
    EQUIPMENT_ID            VARCHAR(30), 

    /*Generic Fields*/
    STATUS                  VARCHAR(1),
    INSPECTION_STATUS       VARCHAR(1),
    REMEDIATION_STATUS      VARCHAR(1),

    /*Specific properties for tank equipment*/
    FLOATING_ROOF_DIAMETER  FLOAT,
    FLOATING_ROOF_THICKNESS FLOAT,

    /*About another additional 50 specific fields*/

    PRIMARY KEY (EQUIPMENT_ID),
    FOREIGN KEY (EQUIPMENT_ID) REFERENCES EQUIPMENT(EQUIPMENT_ID)

);