dpl57372 2017-11-28 09:32
浏览 64
已采纳

当onclick on div并且无法将参数化链接作为innerHTML时,无法将字符串从php传递到javascript

I want to load a file when on click on the different text in another div. I have 5 div. And also I need to fix some default content in all the div. div should override and call the file when clicking on the text which is in another div. I am able to call two files in two different files when I click on the text in the left div. But when I try to pass a string from PHP to javascript I am not able to do that. I want to parameterize the link to the different text. How can I achieve this? If I try to assign a PHP variable to it is giving nothing in output. Thanks in advance.

 <html>
<head>
    <title>A div layout</title>
</head>

<body>

    <script>
            function load_home(link){

                document.write(link);

                var project="<?php echo $name; ?>";

                var table_link='table_'+link+'.php';

                var graph_link='graph_'+link+'.php';


                document.getElementById("tables").innerHTML='<object type="type/html" data="table_link" height=100% width=100%></object>';
                document.getElementById("graph").innerHTML='<object type="type/html" data="graph_link" height=100% width=100%></object>';

            }
    </script>

    <div id="header" style="width: 100%; height:7%; background-color: green; color: white;">Merahkee Tech Solutions
    </div>

    <div id="tree" style="width: 20%; height:88%; background-color: pink; color: white; float:left">
    <?php
    $name="customer";
        echo '<nav>
          <ul>
            <li><a href ="#" onclick="load_home('.$name.')">Pebble</a></li>
            <li><a href ="#" onclick="load_home()">Jenkins</a></li>
            <li><a href ="#" onclick="load_home()">Tomcat</a></li>
            <li><a href ="#" onclick="load_home()">Jira</a> </li>
          </ul>
        </nav>'
    ?>
    </div>

    <div id="tables" style="width: 80%; height:35%; background-color: skyblue; color: white; float:left">This is default content

    </div>

    <div id="graph" style="width: 70%; height:53%; background-color: lightblue; color: white; float:left">This is trial graph div
    </div>

    <div id="dropdown" style="width: 10%; height:53%; background-color: pink; color: white; float:left">This is trial dropdown div
    </div>

    <div id="footer" style="width: 100%; height:5%; background-color: green; color: white; float:left">This is a footer div
    </div>


</body>

Now I am not getting anything once I click on text in the first div.

  • 写回答

2条回答 默认 最新

  • doujiaohuo1096 2017-11-28 09:46
    关注

    You need to put your string in ' or " when you pass it to the javascript function so the function know that you are sending string so you instead of :

    echo '<nav>
              <ul>
                <li><a href ="#" onclick="load_home('.$name.')">Pebble</a></li>
                <li><a href ="#" onclick="load_home()">Jenkins</a></li>
                <li><a href ="#" onclick="load_home()">Tomcat</a></li>
                <li><a href ="#" onclick="load_home()">Jira</a> </li>
              </ul>
            </nav>';
    

    you Do:

    echo "<nav>
              <ul>
                <li><a href =\"#\" onclick=\"load_home('{$name}')\">Pebble</a></li>
                <li><a href =\"#\" onclick=\"load_home()\">Jenkins</a></li>
                <li><a href =\"#\" onclick=\"load_home()\">Tomcat</a></li>
                <li><a href =\"#\" onclick=\"load_home()\">Jira</a> </li>
              </ul>
            </nav>";
    

    And put your script in the end of your body

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置