简体   繁体   English

appendChild()和createElement()Javascript问题

[英]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. 当我在代码中使用appendChild()和createElement()时,未应用已定义CSS ID的后续样式。 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... ) 然后,我还有其他一些代码通过Bing的API处理我的搜索查询(仅因为Google现在收费...)

Next, I use the same methods to insert another div: 接下来,我使用相同的方法插入另一个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 但是,当我将样式应用于searchResultsPage时,例如

#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 就像一个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. ID在页面内应该是唯一的,因此,如果您有两个id =“ searchResultsPage”的元素,则该行为可能会变得有些棘手,并且HTML无效。 Instead, use a class="searchResultsPage" if there will be multiple elements. 如果有多个元素,请使用class =“ searchResultsPage”。

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. 其他评论者指出的缺失“ s”的问题也很重要,尽管希望这只是问题中的一个错字。

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

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