img的jQuery.attr(“src”)不起作用
jQuery .attr("src") for img not working
这让我抓狂。
<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']; ?>");
免责声明:我没有测试它。
相关文章:
- jQuery place attr src在Chrome中不起作用
- setAttribute和video.src用于更改IE9中不起作用的视频标记源
- 如果iframe在src(pdf或text)中有非html文档,则iframe中的onload不起作用
- 相对网址在JavaScript(element.src)中不起作用
- AngularJs ng-include src =“sample.html ” 不起作用
- 保存图像 src 的功能不起作用
- 在图像不起作用的 src 中添加 jQuery 变量
- AngularJS - img ng-src 到 base64 数据(不是 url)不起作用
- 设置图像 SRC 在火狐浏览器上不起作用
- 脚本 SRC 归因有时在.php文件中不起作用
- 奇怪的 img src 路径,在本地主机上不起作用
- 脚本标签的动态创建和 src 加载在 ie10 中不起作用
- document.getElementById('foo').src='' 不起作用
- Javascript set src 属性的图像不起作用
- href 中的角绑定有效,但 src 绑定不起作用
- 改变iframe src的Javascript;不起作用
- 用javascript更改图像src不起作用
- jQuery更新图像src不起作用
- Javascript:更改src不起作用
- 更改iframe的src不起作用