2013-07-16 73 views
0

这让我感到很不爽。jQuery .attr(“src”)对于img不起作用

<div id="HLrec_preview" style="width: 218px;"> 
    <img src="HLrec.jpg" style="width: 218px;"> 
<div style="font-weight: bold;" id="title">a</div> 
<div id="link">a</div> 
<div id="plaintext">a</div> 
</div> 
<script> 
    var HL_rec_image_link = $("#HLrec_preview img").attr("src"); 
    alert(HL_rec_image_link); 
</script> 

代码工作得很好中的jsfiddle http://jsfiddle.net/t72uQ/

但在我的网站,这是行不通的。警报显示为空白。

必须有一些影响结果的外部因素,但我无法弄清楚什么。

这可能是什么原因造成的?

在此先感谢

更完整代码:

<input type="button" id="HLrec_input" value="HL Recommends" style="display:block; width:222px;"/> 
<div id="HLrec_preview" style="width: 218px;"> 
    <img src="" style="width: 218px;"/> 
    <div style="font-weight: bold;" id="title"></div> 
    <div id="link"></div> 
    <div id="plaintext"></div> 
</div> 
<script> 
$(document).ready(function(e) { 

      var HLrec = "<?php 
    if(isset($_GET["ID"])) 
     echo $row["HL_REC"]; 
    else 
     echo "0"; 
     ?>"; 
     if(HLrec == "1") 
     { 
      <?php 
     $query = "SELECT IMAGE_LINK, TITLE, LINK, PLAINTEXT FROM HL_Rec WHERE ID=" . $_GET['ID']; 
     $result = mysql_query($query); 
     $row = mysql_fetch_assoc($result); 
      ?> 

      $("#HLrec_preview img").attr('src', "<?php echo $row["IMAGE_LINK"]; ?>"); 
      $("#HLrec_preview #title").text("<?php echo $row["TITLE"]; ?>"); 
      $("#HLrec_preview #link").text("<?php echo $row["LINK"]; ?>"); 
      $("#HLrec_preview #plaintext").text("<?php echo $row["PLAINTEXT"]; ?>"); 
     } 

$("#submit_input").click(function(e) { 
      //set HLrec data 
    var HL_rec = 0; 
    var HL_rec_title = "none"; 
    var HL_rec_link = "none"; 
    var HL_rec_image_link = $("#HLrec_preview img").attr("src"); 
    var HL_rec_plaintxt = "none"; 
    alert(HL_rec_image_link); 
    if(HL_rec_image_link.length > 0) 
    { 
     HL_rec = 1; 
     HL_rec_title = $("#HLrec_preview #title").text(); 
     HL_rec_link = $("#HLrec_preview #link").text(); 
     HL_rec_image_link = $("#HLrec_preview img").attr('src'); 
     HL_rec_plaintxt = $("#HLrec_preview #plaintext").text(); 
    } 
    else 
     HL_rec_image_link = "none"; 
     }); 
}); 
</script> 

更多的代码! 这个代码叫做页面

//appends the article tags with content 
var title = $("#title").val(); 
var url = $("#link").val(); 
var plaintxt = $("#plaintext").val(); 
//inject html 
$("#HLrec_preview img", parent.document).attr('src', newfile); 
$("#HLrec_preview #title", parent.document).text(title); 
$("#HLrec_preview #link", parent.document).text(url); 
$("#HLrec_preview #plaintext", parent.document).text(plaintxt); 

哎呦一个iframe改变SRC。

有另一行代码被干扰。 这种事发生时就讨厌了。

这是不好的代码

//remove t variable from img src's to re enable caching 
$("#content_reader_collumn img, #submit_form img").each(function(index, element) { 
    $(this).attr("src", $(this).attr("src").substring(0, $(this).attr("src").indexOf('?t='))); 
}); 

只需要改变选择。

感谢您的帮助球员

+2

你会给我们你的网站网址吗? – Emkey

+0

文档就绪处理程序中的代码是什么? – Barmar

+0

是的,特别是如果您怀疑问题不在您已经提供的代码中。 – Luke

回答

0

问题都可以在这里

$("#HLrec_preview img").attr('src', "<?php echo $row["IMAGE_LINK"]; ?>"); 

泰这个代替

$("#HLrec_preview img").attr('src', "<?php echo $row['IMAGE_LINK']; ?>"); 

同样,

$("#HLrec_preview #title").text("<?php echo $row['TITLE']; ?>"); 
$("#HLrec_preview #link").text("<?php echo $row['LINK']; ?>"); 
$("#HLrec_preview #plaintext").text("<?php echo $row['PLAINTEXT']; ?>"); 

声明:我没有测试它。

+2

PHP不应该在意你是使用单引号还是双引号。 – Barmar

+0

是的,这是一个错误,但它不会改变问题。当我提交时,我可以看到src是“HLrec.jpg”。 – Nikita240

+0

@Barmar但不会将“<?php echo $ row [”视为一个字符串 –