duanli9930 2015-09-04 22:16
浏览 40
已采纳

javascript追加Php echo?

I try to append something to a div over a Php echo

Includes

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>

Php Code

<?php echo "<script type='text/javascript' >$('.notificationsBody').prepend('Hello World');</script>"; ?>

My Div Code

<div class="notificationsBody">TESTING DIV<br></div>

Is this possible and if yes why isnt this working ? My Console is empty too. If not is there a other way to realize a Div appending over a Php Echo ?

Hint: I need a Php echo if its possible.

This is my complete index.php Page

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

        "http://www.w3.org/TR/html4/loose.dtd">



<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <title>Test</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>

</head>
<body>


<?php echo "<script type='text/javascript' >$('.notificationsBody').prepend('Hello World');</script>"; ?>
<div class="notificationsBody">TESTING DIV<br></div>

</body>



</html>

What i want at the end is like you have a Class and call the Function

<?php echo $myclass->theechofunction(); ?>

Then this will echo the Javascript which appending a Div to my Wrapper.

Yes its a bit crazy / difficult but thats what i need.

Thanks for every Answer.

EDIT

The Site Quellcode after calling the site

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>



<script type='text/javascript' >$('.notificationsBody').prepend('Hello World');</script>

<div class="notificationsBody">TESTING DIV<br></div>
  • 写回答

1条回答 默认 最新

  • douyu9433 2015-09-04 23:33
    关注

    You have two problems. First, you're loading two versions of jQuery, that will probably cause conflicts. Either load jQuery from ajax.googleapis.com or code.jquery.com, but not both.

    Second, the Javascript that tries to append to .notificationBody is running before you add that element to the DOM. There are two ways to solve this: either move the code that echoes the <script> tag to after you echo the .notificationBody DIV, or run your code in the $(document).ready() handler, which runs after the DOM is completely loaded:

    <?php echo "<script type='text/javascript' >
        $(document).ready(function() {
            $('.notificationsBody').prepend('Hello World');
        });
        </script>";
    ?>
    

    See JS & jQuery can't detect html elements, and say's they are undefined

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c