如何在 PHP 中使用 getElementById

发布于 2024-12-06 05:43:44 字数 382 浏览 2 评论 0原文

我正在尝试选择具有特定 ID 的动态生成页面 (drupal6) 的所有图像,以便我可以应用一个类。

我已经阅读了 php.net 上的 getElementById 和 setIdAttribute 文档,但这些文档对我来说没有多大意义,并且我尝试声明 ID 然后调用它的尝试不起作用。我也浏览了这里的各种类似的帖子,但效果并没有更好。

我实际上只是在寻找一个基本示例,说明如何一起使用这两个函数来获取特定 ID 的图像。

编辑 - 很抱歉我含糊其辞 - 我不能使用 Jquery/javascript (这是我的第一选择)。根据我正在做的事情,我不能冒 FOUC 的风险。 jquery/javascript 将是我唯一的选择吗? - 如果可能的话我更喜欢 PHP

谢谢 斯蒂芬妮

I'm trying to select all of the images of a dynamically produced page (drupal6) that have a specific ID, so I can apply a class.

I've read the docs on php.net for getElementById and setIdAttribute but the docs don't make much sense to me, and my attempts at declaring the ID then calling it don't work. I've also looked through the various similar posts here and have not fared better.

I'm really just looking for a base example of how I'm supposed to use these two functions together to get images of a specific ID.

EDIT -
I'm sorry I was vague - I can't use Jquery/javascript (it was my first choice). based on what I'm doing I can't risk a FOUC. is jquery/javascript going to be my only choice? - I would prefer PHP if possible

Thanks
Stephanie

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梓梦 2024-12-13 05:43:44

这里有一些快速而肮脏的代码作为起点:

$dom = new DOMDocument;
$dom->loadHTML('<p><img src="1.jpg"></p><p><img id="foo" src="2.jpg"></p><p><img src="3.jpg"></p>');

$foo = $dom->getElementById('foo');
if( !is_null($foo) && $foo->nodeName=='img' ){
    $foo->setAttribute('class', 'bar');
    echo $dom->saveHTML();
}

Here you are some quick and dirty code to take as starting point:

$dom = new DOMDocument;
$dom->loadHTML('<p><img src="1.jpg"></p><p><img id="foo" src="2.jpg"></p><p><img src="3.jpg"></p>');

$foo = $dom->getElementById('foo');
if( !is_null($foo) && $foo->nodeName=='img' ){
    $foo->setAttribute('class', 'bar');
    echo $dom->saveHTML();
}
清君侧 2024-12-13 05:43:44

为此,您应该使用 javaScript。 使用 jQuery 非常简单,您可以使用:

<script type='text/javascript'>
  $.ready(function(){
    $(".class_name").css("THE CSS YOU WANT TO APPLY TO THE ELEMENTS WITH THE CLASS 'class_name' ");
  });
</script>

如果您想捕获特定图像,

<script type='text/javascript'>
  $.ready(function(){
    var src_of_img = $("#ID_OF_IMAGE").attr("src");
    alert("the src of the image with the ID you specified is: " + src_of_img);
  });
</script>

You should use javaScript for that. Using jQuery is pretty straightfoward

<script type='text/javascript'>
  $.ready(function(){
    $(".class_name").css("THE CSS YOU WANT TO APPLY TO THE ELEMENTS WITH THE CLASS 'class_name' ");
  });
</script>

if you want to catch a specific image, you can use:

<script type='text/javascript'>
  $.ready(function(){
    var src_of_img = $("#ID_OF_IMAGE").attr("src");
    alert("the src of the image with the ID you specified is: " + src_of_img);
  });
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文