[英]How to pass a php variable value in jquery selector?
我想突出顯示從mysql數據庫中檢索id的映射圖像的那一部分。 我已將id存儲在$ loc變量中。 現在使用jquery選擇器我想在我的jquery選擇器中傳遞這個php變量,這樣它將突出顯示與$ loc中具有相同id的圖像部分。 這是我的工作代碼:
<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">
用#<?php echo $loc ?>
替換#cr1
代替:
$('#cr1').data('maphilight', data).trigger('alwaysOn.maphilight');
采用:
$('#<?php echo $loc ?>').data('maphilight', data).trigger('alwaysOn.maphilight');
您可以使用json_encode
函數並將php變量$loc
作為參數傳遞
例如
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.