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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测