无法通过javascript动态创建img

时间:2015-02-20 07:36:43

标签: javascript

我尝试通过javascript创建img元素并将其设置为td元素,但id不起作用。我看不到任何img未找到的图像或图标。我的服务器现在在localhost上运行,这就是img url

的原因
function fillTable(actualMarkers){
        // Get array of classes without jQuery
        var theTable = document.createElement('table');
        theTable.id = 'actualPlaces';
        // Note, don't forget the var keyword!
        for (var i = 0, tr, tdName, tdId, tdImage, imgPlace ; i < actualMarkers.length; i++) {
            tr = document.createElement('tr');
            tdId = document.createElement('td');
            tdImage = document.createElement('td');
            tdName = document.createElement('td');
            imgPlace = document.createElement('img');
            imgPlace.src = 'http://localhost:8080/webapp/images/var/webapp/photos/small/tuc.png';
            imgPlace.height = '20';
            imgPlace.width = '20';
            var marker = actualMarkers[i];
            tdId.appendChild(document.createTextNode(marker.get("id")));
            tdId.className = 'unvisible';
            tdImage.appendChild(imgPlace);
            tdName.appendChild(document.createTextNode(marker.get("name")));
            tr.appendChild(tdId);
            tr.appendChild(tdImage);
            tr.appendChild(tdName);
            theTable.appendChild(tr);
        }

        document.getElementById('foundPlaces').appendChild(theTable);
        addRowHandlers();
    }

1 个答案:

答案 0 :(得分:0)

通常src属性如下:

 imgPlace.src = 'photos/small/tuc.png';

尝试更改您的图片路径,但我认为在您的src网址中有两次“webapp”非常奇怪。