开发者

How to use getElementById in 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


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();
}


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜