javascript - CSS 未被另一个函数调用的函数使用

标签 javascript css function

下面代码中的问题是从另一个函数调用一个函数,然后所有格式都丢失了 - 函数 domenu 使用相关页码调用 doarticle OK,但 doarticle 函数根本不使用 CSS 进行格式化。如果我独立调用这些函数,那么一切正常,只有当我从另一个调用一个时,问题才会出现。

使用 FIREBUG 监控 CSS:调用 doarticle 函数时,所有 CSS 数据都会从 DOM 中消失。

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta http-equiv="X-UA-Compatible" content="chrome=1,IE=9" />
<meta name="viewport" content="user-scalable=no, width=768" />
<link rel="apple-touch-icon" href="images/guide_icon.png"/>
<title>The Guide</title>
<style type="text/css">
    body
    {
    width:768px;
    background: #FFFFFF;
    border-width: 0px;
    border-style: solid;
    border-color: #FFFFFF;
    padding: 0;
    margin: auto;
    position:relative;
    }
    div, span
    {
    font-family:Arial,Helvetica,sans-serif;
    font-size:140%;
    font-weight:normal;
    text-align:justify;
    color:#000000;
    }
    img.imgpadded
    {
    padding: 12px 7px 7px 7px ;
    }
    img.imgborder
    {
    width:764px;
    border-width: 2px;
    border-style: solid;
    border-color: #FFFFFF;
    }
    span.footnum
    {
    font-family:Arial,Helvetica,sans-serif;
    font-size:110%;
    font-weight:normal;
    color:#FF3300;
    }
    span.foottext
    {
    font-family:Arial,Helvetica,sans-serif;
    font-size:110%;
    font-weight:normal;
    color:#000000;
    }
    div.topic
    {
    font-family:Arial,Helvetica,sans-serif;
    font-size:100%;
    font-weight:bold;
    color:#6D8BE6;
    }
    div.title
    {
    font-family:Arial,Helvetica,sans-serif;
    font-size:250%;
    font-weight:bold;
    color:#000000;
    }
    div.summary
    {
    font-family:Arial,Helvetica,sans-serif;
    font-size:115%;
    font-weight:normal;
    color:#000000;
    }
    span.menutitle
    {
    font-family:Arial,Helvetica,sans-serif;
    font-size:100%;
    font-weight:bold;
    color:#6D8BE6;
    }
    span.menusummary
    {
    font-family:Arial,Helvetica,sans-serif;
    font-size:80%;
    font-weight:normal;
    color:#000000;
    }
    div.bigquote
    {
    font-family:Arial,Helvetica,sans-serif;
    font-size:210%;
    font-weight:normal;
    color:#000033;
    }
    img.banner
    {
    width: 768px;
    vertical-align: bottom;
    }
    sup
    {
    font-family:Arial,Helvetica,sans-serif;
    font-size:50%;
    font-weight:bold;
    color:#FF3300;
    }
    blockquote
    {
    font-family:Arial,Helvetica,sans-serif;
    font-size:100%;
    font-weight:normal;
    color:#000000;
    margin:20px 0 20px 20px;
    padding:10px 10px 10px;
    background-color:#eee;
    border-left:4px solid #6D8BE6;
    }
    ul
    {
    list-style-type:none;
    padding:10px;
    margin:0px;
    }
    li
    {
    background-image:url(images/blue_square.png);
    background-repeat:no-repeat;
    background-position:0px 5px; 
    padding-left:30px;
    padding-right:50px;
    }
</style>

上面的元数据和 CSS

文章下面的HTML渲染引擎...

<script type="text/javascript">
function doarticle (counter)
{
// document.body.innerHTML = "";
hhgaudio = 0;
strpos = 0;
hhgstring = "";

    if (hhgdata[counter][2].substring(strpos,(strpos+7)) === "<HHGML>")
    {
        strpos = 7;
        if (hhgdata[counter][2].substring(strpos,(strpos+8)) == "<BANNER>") // large banner at the top of the article
        {
            strpos = strpos +8;

            while (hhgdata[counter][2].substring(strpos,(strpos+9)) != "</BANNER>")
            {
                hhgstring = hhgstring + hhgdata[counter][2].substring(strpos,(strpos+1));
                strpos++;
            }
            strpos = strpos + 9;
            hhgstring = "<img id=\"banner\" src=\"" + imgpath + hhgstring + "\" style=\"vertical-align: top\"/>";
        }   
        hhgstring = "<img class=\"banner\" src=\"" + imgpath + "hhg_entry.jpg\" style=\"vertical-align: bottom\"/><br/>" + hhgstring;
        document.write(hhgstring);
        hhgstring = "<table frame=\"void\" border=\"0\" cellpadding=\"2\"><tr><td>";        
        hhgstring = hhgstring + "<div class=\"topic\">"+ (hhgdata[counter][0].toUpperCase()) + "<br/></div>"; // main article topic
        hhgstring = hhgstring + "<div class=\"title\">" + hhgdata[counter][1] + "</div>"; // main article title
        hhgstring = hhgstring + "<div class=\"summary\">" + (hhgdata[counter][3].toUpperCase()) + "</div>"; // article sub heading
        hhgstring = hhgstring + "</td></tr></table>"
        document.write(hhgstring);
        hhgstring = "";
        footernum = 0;


    }   
    document.write("<br/><table border=\"0\" cellpadding=\"10\"><tr><td><span>Entry Ends.</span></tr></table><br/>");
} // end of <HHGML> 

</script>

菜单列表函数...

<script type="text/javascript">
function domenu (lastrecord)
{
    hhgstring = "<br/><br/><table border=\"0\" cellpadding=\"0\">";
    for (counter=1; counter<lastrecord; counter++)
    {
        recnum = lastrecord - counter;
        // hhgstring = hhgstring + "<tr><td><img id=\"imgpadded\" src=\"" + imgpath + hhgdata[recnum][4] + "\" align=\"left\"></td>";
        hhgstring = hhgstring + "<tr><td>" + hhgdata[recnum][4] + "</td>";
        hhgstring = hhgstring + "<td><span class=\"menutitle\" onclick=\"doarticle('" +recnum+ "');\">" + tab + hhgdata[recnum][1] + "<br/></span>";
        hhgstring = hhgstring + "<span class=\"menusummary\">" + tab + hhgdata[recnum][3] + "</span></td></tr>";
    }
    hhgstring = hhgstring + "</table>";
    document.write(hhgstring);
    // return(hhgselect); // older idea now depreciated.
}
</script>

</head>

<body>

数据库位...

<?php
    $con = mysql_connect("XXXX","XXXX","XXXX");
    mysql_select_db("HHG", $con);
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }
    else 
    {
        $result = mysql_query("SELECT * FROM articles", $con);
        $numrows = mysql_num_rows($result);
    }
?> 

<script type="text/javascript">
    numrows = <?php echo $numrows; ?>;
    hhgdata = new Array(numrows);
    hhgfooter = new Array(10);
</script>

<?php
    $counter = 0;
    while ($row = mysql_fetch_assoc($result))
    {
        $hhgtitle = $row["hhgtitle"];
        $hhgtopic = $row["hhgtopic"];
        $hhgarticle = mysql_real_escape_string($row["hhgarticle"]);
        $hhgsummary = mysql_real_escape_string($row["hhgsummary"]);
        $hhgidimg = $row["hhgidimg"];
    ?>

将 PHP 转换为 Javascript 变量...

    <script type="text/javascript">
        counter = <?php echo $counter; ?>; 
        hhgtitle = <?php echo '"'.$hhgtitle.'"'; ?>;
        hhgtopic = <?php echo '"'.$hhgtopic.'"'; ?>;
        hhgarticle = <?php echo '"'.$hhgarticle.'"'; ?>;
        hhgsummary = <?php echo '"'.$hhgsummary.'"'; ?>;
        hhgidimg = <?php echo '"'.$hhgidimg.'"'; ?>; 
        hhgdata[counter] = new Array(5);
        hhgdata[counter][0]= hhgtopic;
        hhgdata[counter][1]= hhgtitle;
        hhgdata[counter][2]= hhgarticle
        hhgdata[counter][3]= hhgsummary;
        hhgdata[counter][4]= hhgidimg;
    </script>

    <?php
    $counter++;
    }
mysql_close($con);
?>

调用菜单脚本...

<script type="text/javascript">
imgpath = "images/";
tab = "&nbsp;&nbsp;&nbsp;&nbsp;";
hhgselect = domenu (numrows);
doarticle (hhgselect);
// document.body.innerHTML = ""; // clears the screen


</script>

</body>

</html>

最佳答案

在“domenu”函数中,您将返回一个变量 hhgselect。但是这个变量没有在那个函数中使用,也没有通过 php 初始化。请验证。

关于javascript - CSS 未被另一个函数调用的函数使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8991713/

相关文章:

c++ - 帮助将 C++ 代码转化为函数

javascript - 如何将 javascript 中的 html 设置为元素中的特定位置

javascript - jQuery 在滚动时添加和删除类

CSS - 显示图像 :table-cell expands beyond parent

function - 是否有一个函数接受参数列表列表并将每个列表应用于给定函数?

javascript - 我已经设法找到一种方法来通过使用每个幻灯片来获取我的幻灯片的文本,但是如何让它显示我的 jar 头产品而不是文本?

javascript - Onsen-ui Carousel 属性自动播放不起作用

javascript - 需要重新加载页面才能加载 JavaScript?

javascript - 无法获取映射的映射以返回值

javascript - 幻灯片 : adding a div for box-shadow effects has jQuery selecting the div 的 jQuery 帮助