简体   繁体   English

无法使用javascript在我的img中设置标题和alt属性

[英]Cannot set title and alt attributes in my img using javascript

I'm using the Google Image Search API, and largely copied this code, which builds the search results in my page. 我正在使用Google Image Search API,并在很大程度上复制了此代码,从而在页面中构建了搜索结果。

for (var i = 0; i < results.length; i++) {
    // For each result write it's title and image to the screen
    var result = results[i];
    var linkContainer = document.createElement('div');

    var title = document.createElement('div');
    // We use titleNoFormatting so that no HTML tags are left in the title
    title.innerHTML = result.titleNoFormatting;

    var newImg = document.createElement('img');

    newImg.src = result.tbUrl;
    newImg.className = 'googleSearchResult';
    newImg.title = 'newTitle';
    newImg.alt = 'newAlt';

    var newLink = document.createElement('a');
    newLink.href = "temp_url";              

    newLink.appendChild(newImg);
    contentDiv.appendChild(newLink);
}

In the following lines, 在以下几行中

newImg.src = result.tbUrl;
newImg.className = 'googleSearchResult';
newImg.title = 'newTitle';
newImg.alt = 'newAlt';

the first two, which set the image src and class, work fine, but the second two, which should set the title and alt of the image, don't work at all. 前两个设置图像的src和class可以正常工作,但是后两个设置图像的标题和alt则根本不起作用。 Can anyone see why this would be happening? 谁能看到为什么会这样? Thanks for reading. 谢谢阅读。

EDIT: Here is the HTML when inspected in Firefox through Firebug: 编辑:这是通过Firebug在Firefox中检查时的HTML:

<img alt="" src="http://images.google.com/images?q=tbn:_HZix2CrLSLSOM::bestonlinetvseries.com/deadwood/deadwood_s01.jpg" class="googleSearchResult">

Here is the HTML when inspected in Google Chrome 这是在Google Chrome浏览器中进行检查时的HTML

<img alt src="http://images.google.com/images?q=tbn:_HZix2CrLSLSOM::bestonlinetvseries.com/deadwood/deadwood_s01.jpg" class="googleSearchResult">

EDIT: What I am trying to do here is store an extra bit of data in the HTML for the image, to be used in a jQuery plugin later. 编辑:我在这里想要做的是在图像的HTML中存储额外的数据,以便稍后在jQuery插件中使用。 If anyone can suggest an alternative way to do this, that would be great as well. 如果有人可以提出其他替代方法,那也很好。

I suspect the problem lies elsewhere. 我怀疑问题出在其他地方。 Consider this example , which works fine in the browsers I've tested (Firefox, Chrome, IE): 考虑以下示例该示例在我测试过的浏览器(Firefox,Chrome,IE)中运行良好:

var link = document.createElement("a");
link.href = "http://stackoverflow.com";

var img = document.createElement("img");
img.title="Hello";
img.alt="alt Hello";
img.src="http://jsfiddle.net/img/sprites.png";
link.appendChild(img);
document.body.appendChild(link);

alert(img.getAttribute("alt"));
alert(img.getAttribute("title"));

I would recommend checking to see if anything might be in front of the link/img element, as this would stop a tooltip from appearing on hover. 我建议您检查一下link / img元素前面是否有任何东西,因为这会阻止工具提示出现在悬停时。

Try: 尝试:

img.setAttribute('title', 'someTitle');

Appearantly, there's a difference between 'properties' and 'attributes' in DOM scripting. 显然,DOM脚本中的“属性”和“属性”之间存在差异。

为了响应您的上一次编辑:如果您只是想存储与图像关联的数据以供以后使用,则可以使用jQuery的data方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM