img的jQuery.attr(“src”)不起作用

jQuery .attr("src") for img not working

本文关键字:src 不起作用 jQuery attr img      更新时间:2023-09-26

这让我抓狂。

<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>

更多代码!此代码更改页面中调用的iframe的src

//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);

哇。

有另一行代码干扰了。当这种情况发生时,我很讨厌。

这是错误的代码

//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=')));
});

只需要改变选择器。

感谢大家的帮助

问题可能在这里

$("#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']; ?>");

免责声明:我没有测试它。