doukeng7426 2014-07-07 10:19
浏览 76
已采纳

PHP变量作为HTML元素ID

I'm trying to make a news site (updating an existing one, really based on CMSMS, which has lots of autogenerated code that seems to cause all sorts of problems.) anyhow, I'm updating a template, where, instead of going to a new site where the article belonging to the resume is posted, I would like the resume to hide the div with the resume text in it at the click of a "read more" button. I know how to make the toggle button and all that, the issue is several resumes at the same page needing unique ID's that are set in the template automatically. The idea i had was to generate them with:

<?php $firstId = uniqid(); $secondId = uniqId(); ?>

Then I'd call them like so:

<div id="<?php echo $firstId; ?>" class=""> perhaps <span>here </span> or whatevs.</div> 

And then something Similar for the second Id. The idea of doing it this way is that i'll be able to target these Divs from JS by simply aiming is at the $firstId fx.

however, if i rightclick and inspect the element with firebug i just get

id="<?php echo $firstId; ?>"

instead of:

id="someUniqueId"

So the question is how do i get it to actually parse the code so i can call the element by the ID? e.g. set its visibility to hidden.

  • 写回答

3条回答 默认 最新

  • dongqian5639 2014-07-21 06:55
    关注

    I ended up using a for loop going through each element one by one, targeting them by their css classes, which in this case are just empty.

    as i have access to control how many objects will be shown per page, as well as the templates of both full articles and summaries, i could use simple numbers with the certainty that they'd be unique each time.

    The script simply goes through the page giving all the buttons within the div containing the elements i'm targeting, a value of 0-9. Then it gives the Divs containing the full articles an ID of the current button's ID + 40 resulting in them going from 40-49

    and the summaries then going from 20-29

    Then the rest of the code merely tagerts specific elements, using the button ID initially, then just subtracting/adding to the value in two different variables, to have the article id and the summary id, then i work my way from there. It works perfectly, even if it is a bit simple.

    The inspiration came from your answer Fleshgrinder, thanks :)

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

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?