简体   繁体   中英

Creating a rollover with PHP and Javascript

I've read a TON of question and answers about rollovers using PHP and Javascript and can't figure out why this isn't working. So I have this PHP code:

$rollover = '$("#' . $godName . '").mouseenter(function() {
    $("#' . $godName . '").attr("src","img/gods/god_cards/dark/' . $godImage . '2.png");
});
$("#' . $godName . '").mouseleave(function() {
    $("#' . $godName . '").attr("src","img/gods/god_cards/' . $godImage . '.png");
});';

$godName and $godImage are variables retrieved through a query to a database. I wanted to use this code to cut down on the amount I'd have to write to create a rollover for over 50 images, so basically it would create a function for every image retrieved in the query. Then I wanted to stick it in a javascript tag like so:

<script type="text/javascript"> 
    <?php echo $rollover; ?>
</script>

But it doesn't work. And I've tried doing it with and without the echo and no version of whatever I've found on here or anywhere else works. When I echo it out as just text it looks fine, it spits out what it should so I'm not sure why the javascript just doesn't seem to be accepting the php. Any help?

Do you check if the HTML Dom is ready? You are using jQuery, so you can use:

<script type="text/javascript">
$(function(){ 
      <?php echo $rollover; ?>
});
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM