make javascript .click(function() with php
make javascript .click(function() with php
我正在尝试制作一系列javascript。
$("document").ready(function()
{
$('#nav > li > a').click(function(){
if ($(this).attr('class') != 'active'){
$('#nav li ul').slideUp();
$(this).next().slideToggle();
$('#nav li a').removeClass('active');
$(this).addClass('active');
}
else{
$(this).next().slideToggle();
$(this).removeClass('active');
}
})
<?php
include_once '../admin/Clases/sql.php';
$sql = new sql();
$res = $sql->check("products","*","1");
$javascript = "";
if($res["ok"]){
foreach($res["tabla"] as $fila)
{
$javascript .="$('#btn_".fila[2]."').click(function(){$('#prod_img').css('background-image','url(".fila[3].")');})";
}
echo $javascript;
}
?>
但是当我运行它不工作。我做错了什么?
我相信你在JavaScript字符串的末尾缺少一个分号:
$javascript .="$('#btn_".fila[2]."').click(function(){$('#prod_img').css('background-image','url(".fila[3].")');});";
// --------------------------------------------------------------------------------------------------------- HERE ^
也:
- 您忘记了
$fila
php变量 的 - 你必须在php字符串上使用单引号,并在js中反转(或转义)引号。在双引号内,php尝试对变量求值& & &;任何以
$
开头的东西(猜猜jQuery函数以什么开头…)。
$
所以这应该最终工作:
$javascript .= '$("#btn_'.$fila[2].'").click(function(){$("#prod_img").css("background-image","url('.$fila[3].')");});';
然而它丑得要命。我会通过将数据作为JSON传递给js来解决这个问题,而不是从php编写js代码。
相关文章:
- JavaScript with PHP SESSION
- InnerHTML with PHP
- Ajax call VERY SLOW with PHP
- jQuery load with php in it?
- .post javascript with PHP 以启用 select 语句返回
- RSA Ciphering with JavaScript and Deciphering with php opens
- CanvasJs dynamic Data with PHP, mySQL
- writing JavaScript with PHP
- "iPad app lookAndFeel" with php website
- Ajax and jQuery with PHP
- MySQL Security with PHP, javascript, and HTML?
- jQuery UI Autocomplete append with PHP and MySQL
- Ajax cross domain with PHP
- HTML5 Notification With PHP
- jquery onchange with php
- javascript timing with php
- Are canvas.toDataURL compatible with PHP's base64_decode
- Javascript window.open() with php code
- Amazon S3 Multi-Image Upload with PHP and Javascript
- Generating JavaScript with PHP