innerHTML没有收到他的cookie标题

时间:2012-04-08 11:22:06

标签: javascript jquery cookies

我有两个文件,一个名为“TitleLinkCookie.html”的文件,当我点击一些链接时会创建一个名为“NewTitle”的cookie。在我们的例子中使用值innerHTML,“Hello world php 1”/2/3并且运行良好。第二个文件名为“showCodePhp.html”,它需要接收cookie标题并将其显示在h3标签id =“TitleField”,innerHTML中,由于某种原因它不能正常工作。谢谢你的帮助。

TitleLinkCookie.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<head>
<META NAME="Description" CONTENT="Guide,Tutorial">
    <meta name="keywords" content="HTML,CSS,JavaScript, jQuery,Html5,Framework,php,photoshop,design" />
    <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<title>Link guide</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.js'></script>
<STYLE type="text/css">
   a { color:red; margin:5px; cursor:pointer; }
  a:hover { background:yellow; }?
 </STYLE>
<script type="text/javascript">
        $(document).ready(function() {
        $('a').each(function() {
                        $(this).click(function() {
                            var   varibaleTitle=$(this).html(); 
                                    setCookie(varibaleTitle);
                        });
  });
  function setCookie(varibaleTitle){
      var expireDate = new Date();
        expireDate.setMonth(expireDate.getMonth()+1);
     var NewTitle = varibaleTitle;
      document.cookie = "NewTitle=" + NewTitle + ";path=/;expires=" + expireDate.toGMTString();
    alert(varibaleTitle)};//end function setCookie
   });// document ready
</script>
</head>
<body >
<p><a id="link1" href="showCodePhp.html" >Hello world php 1</a></p>
<p><a id="link2" href="showCodePhp.html" >Hello world php 2</a></p>
<p><a id="link3" href="showCodePhp.html" >Hello world php 3</a></p>
</body >
</html>

showCodePhp.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Hello World</title>
    <!--<script src="http://www.centerwow.com/gotemp/gotemptics.js" type="text/javascript"></script>-->
    <script type="text/javascript" src="scripts/shCore.js"></script>
    <script type="text/javascript" src="scripts/shBrushPhp.js"></script>
    <link type="text/css" rel="stylesheet" href="styles/shCoreDefault.css"/>
    <script type="text/javascript">SyntaxHighlighter.all();</script>
    <script type="text/javascript">
        $(document).ready(function() {
        function reedCookie() {
         var str =document.cookie.split("=")[1];
         var str2 = document.getElementById("TitleField").innerHTML;         
         if (document.cookie != "") {
            str2 = "Your Title is : " + str;
        }//end reedCookie
        });// document ready
    </script>
</head>
<body style="background: white; font-family: Helvetica">
<p><a href="http://www.centerwow.com/guide/guide.html">Back to Home</a></p>
<p><h3 id="TitleField"></h3>Title : </p>
<pre class="brush: php;">
<?
// Hello world in PHP
 print("Hello World");
?>
</pre>
<body>
</html>

1 个答案:

答案 0 :(得分:1)

你声明你需要更改HTML元素“TitleField”的innerHTML,但实际上你没有设置它!。

我修改了你的脚本以在下面进行设置:

<script type="text/javascript">
    $(document).ready(function() {
    function reedCookie() {
     var str =document.cookie.split("=")[1];
     var str2 = document.getElementById("TitleField").innerHTML;         
     if (document.cookie != "") {
        $("#TitleField").html("Title :"+ str );
    }//end reedCookie
    });// document ready
</script>