duanli12176 2011-10-31 15:25
浏览 16
已采纳

是否可以使用JS / PHP动态地为网页中的元素分配ID?

can i set the id of an element programatically, as in a dynamic setting of id of an element, during runtime of the webpage?

Consider this scenario -

<body id="body1">

Now, i want to set the id of this element "body" dynamically, using the value of a variable in the php code. Is this possible? something like -

<body id=" <?php echo $bodyID; ?> ">

I use php and jquery in the page; please let me know whether such dynamic id assignment is possible to the elements in the html.

thanks.

  • 写回答

3条回答 默认 最新

  • dongtieshang5429 2011-10-31 15:32
    关注

    PHP is the background language, it's what produces your HTML output. So basically, what ever you output from PHP eventually becomes your HTML, meaning yes you can use PHP variables as your elemnt-ID or anything else for that matter.

    <?PHP
    $var = "body1";
    ?>
    <body id="<?PHP echo $var; ?>"> Hello my ID is <?PHP echo $var; ?></body>
    

    OR You can output all of the HTML using a single echo statement.

    <?PHP
    $var = "body1";
    echo "<body id='$var'>Hello my ID is $var</body>";
    ?>
    

    In conclusion, whatever is left after PHP is finished executing is your HTML code that the end users browser interprets... Hope this helps...

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

报告相同问题?

悬赏问题

  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上