Javascript 如何将变量作为jQuery.attr的值传递?

Javascript 如何将变量作为jQuery.attr的值传递?,javascript,jquery,Javascript,Jquery,使用合适的语法将变量作为值传入。我想做的是: var under700 = 'image.jpg' $('.two_images img').attr('src', (under700) ); 编辑 从回答来看,我可能把这件事简单化了。下面是函数的其余部分,如果这增加了清晰度的话 $(document).ready(function() { function imageresize() { var contentwidth = $('#two_up').w

使用合适的语法将变量作为值传入。我想做的是:

var under700 = 'image.jpg'

$('.two_images img').attr('src', (under700) );
编辑 从回答来看,我可能把这件事简单化了。下面是函数的其余部分,如果这增加了清晰度的话

    $(document).ready(function() {
     function imageresize() {
         var contentwidth = $('#two_up').width();
         var under700 = '<?php bloginfo('template_url'); ?>/scripts/timthumb.php?src=<?php echo $image_img_tag[0] ?>&w=340'
         if ((contentwidth) < '700'){
     // I also tried defining the variable here
             $('.two_images img').attr('src', under700 );
             } else { 
     // this one works fine
             $('.two_images img').attr('src','<?php echo $image_img_tag[0] ?>');
             }
     }
 imageresize();//Triggers when document first loads     
 $(window).bind("resize", function(){//Adjusts image when browser resized
 imageresize();
});
$(文档).ready(函数(){
函数imageresize(){
var contentwidth=$('two#u up').width();
var under700='/scripts/timtumb.php?src=&w=340'
如果((内容宽度)<'700'){
//我还尝试在这里定义变量
$('.two_images img').attr('src',700下);
}否则{
//这个很好用
$('.two_images img').attr('src','');
}
}
imageresize();//首次加载文档时触发
$(窗口).bind(“resize”,function(){//调整浏览器大小时调整图像
imageresize();
});

不需要内括号,尽管它们不会停止它的工作。

您是否尝试过不使用php的代码?这行单引号太多可能会有一些问题,但是如果不查看文档的其余部分,就很难判断


尝试用普通字符串替换php回音,例如:
var under700='123';
,并确保变量声明行结尾有一个分号,它看起来可能也丢失了,并且可能会导致奇怪的错误。

我相信这行是错误的:

if ((contentwidth) < '700'){
试试看。


<script>
  // declare early
  function imageresize() {
    var contentwidth = $('#two_up').width();
    // double quoted: "template_url" and missing ; at the end of line
    var under700 = '<?php bloginfo("template_url"); ?>/scripts/timthumb.php?src=<?php echo $image_img_tag[0] ?>&w=340';
    // extra parentheses for contentwidth are not necessary
    if (contentwidth < 700)
      $('.two_images img').attr('src', under700 );
    else
      $('.two_images img').attr('src','<?php echo $image_img_tag[0] ?>');
  }

  // run after document is ready
  $(document).ready(function() {
    imageresize();
  });
</script>
//提前申报 函数imageresize(){ var contentwidth=$('two#u up').width(); //双引号:“template_url”且缺失;在第行末尾 var under700='/scripts/timtumb.php?src=&w=340'; //contentwidth不需要额外的括号 如果(内容宽度<700) $('.two_images img').attr('src',700下); 其他的 $('.two_images img').attr('src',''); } //文档准备好后运行 $(文档).ready(函数(){ imageresize(); });
虚拟示例:

<?php
// test.php
function bloginfo($a) {
  echo "http://somebloginfo.com/qwerty";
} 

$image_img_tag = array("someimagtag.jpg");

?>

<html>
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
    <script>

    function imageresize() {
      var contentwidth = $('#two_up').width();
      var under700 = '<?php bloginfo("template_url"); ?>/scripts/timthumb.php?src=<?php echo $image_img_tag[0] ?>&w=340';
      if (contentwidth < 700)
        $('.two_images img').attr('src', under700 );
      else
        $('.two_images img').attr('src','<?php echo $image_img_tag[0] ?>');
    }

    $(document).ready(function() {
      imageresize();
    });
    </script>
  </head>
  <body>
    test
    <div id='two_up' style="width: 400px;"></div>
    <div class="two_images">
      <img />
      <img />
    </div>
  </body>
</html>

函数imageresize(){
var contentwidth=$('two#u up').width();
var under700='/scripts/timtumb.php?src=&w=340';
如果(内容宽度<700)
$('.two_images img').attr('src',700下);
其他的
$('.two_images img').attr('src','');
}
$(文档).ready(函数(){
imageresize();
});
测试

这应该可以。您可以省略700下
的括号。只需确保变量定义在相同的可见性上下文中尝试将代码添加到
document ready
块中,以防万一……代码是否完全从代码中复制和粘贴?@Richard..我省略了一行..现在添加到$(窗口).bind事件。我认为这与此无关,因为我在函数外部尝试了此操作,当我检查源代码时,它仍然只是在写入变量名我先尝试了此操作。它只是打印了此操作。它没有传递变量。谢谢。我应该将完整的函数放在开头。出现了其他问题。谢谢。我尝试了123(和;),但它只是在700下打印。您尝试过使用
console.log(700下)
console.log(contentwidth)吗
,只是想看看它们是否符合您的期望?不是。我刚刚意识到我运行的是一个非常旧的jQuery版本。v1.4.4,也许这就是问题所在?我刚刚添加了一些虚拟示例页面,请复制粘贴并检查它是否适用于您:)
<script>
  // declare early
  function imageresize() {
    var contentwidth = $('#two_up').width();
    // double quoted: "template_url" and missing ; at the end of line
    var under700 = '<?php bloginfo("template_url"); ?>/scripts/timthumb.php?src=<?php echo $image_img_tag[0] ?>&w=340';
    // extra parentheses for contentwidth are not necessary
    if (contentwidth < 700)
      $('.two_images img').attr('src', under700 );
    else
      $('.two_images img').attr('src','<?php echo $image_img_tag[0] ?>');
  }

  // run after document is ready
  $(document).ready(function() {
    imageresize();
  });
</script>
<?php
// test.php
function bloginfo($a) {
  echo "http://somebloginfo.com/qwerty";
} 

$image_img_tag = array("someimagtag.jpg");

?>

<html>
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
    <script>

    function imageresize() {
      var contentwidth = $('#two_up').width();
      var under700 = '<?php bloginfo("template_url"); ?>/scripts/timthumb.php?src=<?php echo $image_img_tag[0] ?>&w=340';
      if (contentwidth < 700)
        $('.two_images img').attr('src', under700 );
      else
        $('.two_images img').attr('src','<?php echo $image_img_tag[0] ?>');
    }

    $(document).ready(function() {
      imageresize();
    });
    </script>
  </head>
  <body>
    test
    <div id='two_up' style="width: 400px;"></div>
    <div class="two_images">
      <img />
      <img />
    </div>
  </body>
</html>