简体   繁体   English

如何在jquery选择器中传递php变量值?

[英]How to pass a php variable value in jquery selector?

I want to highlight that portion of a mapped image whose id is retrieved from mysql database. 我想突出显示从mysql数据库中检索id的映射图像的那一部分。 I have stored the id in $loc variable. 我已将id存储在$ loc变量中。 Now using jquery selectors I want to pass this php variable in my jquery selector so that it will highlight the portion of image having the same id as in $loc. 现在使用jquery选择器我想在我的jquery选择器中传递这个php变量,这样它将突出显示与$ loc中具有相同id的图像部分。 Here is my working code: 这是我的工作代码:

    <script type="text/javascript" src="jquery.js"></script>
 <script type="text/javascript" src="jquery.maphilight.js"></script>
<script type="text/javascript">
 $(function () {
        $(".mapping").maphilight();


    $(".hoverable").css("border", "3px solid red");


    var data = $('#cr2').data('maphilight') || {};  
    data.alwaysOn = !data.alwaysOn;
    $('#cr1').data('maphilight', data).trigger('alwaysOn.maphilight'); // portion to always highlight (I WANT TO PASS THE PHP VARIABLE $LOC INSTEAD OF '#CR1'

});<area shape="poly" id="cr1" class="room" coords="549,272,489,334,534,377,594,316" href="www.msn.com" target="_top" alt="CR1" name="room-1">

Replace #cr1 with #<?php echo $loc ?> #<?php echo $loc ?>替换#cr1

Instead of: 代替:

$('#cr1').data('maphilight', data).trigger('alwaysOn.maphilight');

Use: 采用:

$('#<?php echo $loc ?>').data('maphilight', data).trigger('alwaysOn.maphilight');

You can use json_encode function and pass the php variable $loc as a parameter 您可以使用json_encode函数并将php变量$loc作为参数传递

For example 例如

var data = <?php echo json_encode($loc); ?>;

jQuery.each(data, function(i,event) {
   // alert(event.toSource());
   var id = event.id;  // here id is the array of the value based on the php variable for $loc
   badge_name = event.name; // here name is the array of the value based on the php variable for $loc
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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