开发者

appendChild() and createElement() Javascript question

When I use appendChild() and createElement() in my code, the subsequent styles for the defined CSS IDs are not applied. Can someone tell me why? Here's my code:

function searchDone(results) {
    var result = null;
    var parent = document.getElementById('postWrap');
    var child = null;
    parent.innerHTML = '';
    var insertHTML =" ";

    //Paginating Results Links
    resultNum = results.SearchResponse.Web.Total;
    resultNum = resultNum/10;
    child = document.createElement('div');
    child.id = "paging";
    if(results.SearchResponse.Web.Offset != 0){
        insertHTML ='<span><a class="jsonp b" href="#" rev="'+(results.SearchResponse.Web.Offset-10)+'">&lt;</a></span>';
    }
    if(results.SearchResponse.Web.Offset == 0){
        insertHTML += '<span>1</span>';
    }else{
        insertHTML +='<span><a class="jsonp" href="#" rev="0">1</a></span>';
    }
    for(var i = 1; i <= resultNum; i++){
        if((results.SearchResponse.Web.Offset/10) == i){
            insertHTML += '<span>'+(i+1)+'</span>';
        }else{
            insertHTML += '<span><a class="jsonp b" href="#" rev="'+i*10+'">'+(i+1)+'</a></span>';
        }
    }
    if(results.SearchResponse.Web.Total - results.SearchResponse.Web.Offset > 10){
        insertHTML += '<span><a class="jsonp b" href="#" rev="'+(results.SearchResponse.Web.Offset+10)+'">&gt;</a></span>';
  开发者_运维知识库  }
    child.innerHTML = insertHTML;
    parent.appendChild(child);

I then have some other code which processes my search query via API to Bing (only because Google now charges... )

Next, I use the same methods to insert another div:

//Insert Paginating results again
    child = null;
    child = document.createElement('div');
    child.innerHTML = insertHTML;
    child.id = "searchResultsPages";
    parent.appendChild(child);

Now I'd like to apply some styles to these numbers. However, when I apply a style to searchResultsPage, like

#searchResultsPages{
 float: right;
}

I don't get the style being passed on. The curious thing is that if I only insert one of these two elements, everything goes as planned and the style shows up fine. The problem is that I'd like pages displayed at the top and bottom of the search.

Any ideas why this is happening? I think it might have something to do with an element being used twice, but I don't know why this would effect anything if the objects are different.

Thanks.


child.id = "searchResultsPages";

#searchResultsPage{

See anything wrong there? :)

Like an s


IDs should be unique within the page so if you have two elements with id="searchResultsPage" the behaviour can get a bit screwy and the HTML is invalid. Instead, use a class="searchResultsPage" if there will be multiple elements.

The issue of the missing 's' the other commenters point out is also quite important though hopefully that was just a typo in the question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜