为什么返回getElementsBy **不起作用?

时间:2016-05-21 09:20:12

标签: javascript function

HTML:

def openUrl(source):
    try:
        return urllib2.urlopen(source)
    except urllib2.HTTPError, e:
        print e.code
        time.sleep(5)
        return openUrl(source)
    except urllib2.URLError, e:
        print e.reason
        time.sleep(5)
        return openUrl(source)

if __name__ == '__main__':
    seed = 'y/Yu:Philip_S='
    hdr = {'User-Agent': 'super happy flair bot by /u/spladug'}
    name_index = codecs.open('my_authors_index.txt', 'w', 'utf-8')
    graphml = codecs.open('dblp_conf.graphml', 'w', 'utf-8')

    implementation = DOMImplementation()
    graph_doc = implementation.createDocument("", "graph", "")
    graph = graph_doc.documentElement
    graph.setAttribute("id", "G")
    graph.setAttribute("edgedefault", "undirected")
    author_queue=[]
    author_visited = []
    name_visited = []
    author_dic = {}
    author_visited.append(seed)
    author_queue.append(seed)
    while author_queue:
        seed = author_queue.pop(0)
        print seed
        source = 'http://dblp.uni-trier.de/pers/xx/' + seed + '.xml'
        infield_flag = 0
        conf_dic = {}
        for conf_id in conf:
            conf_dic[conf_id] = 0
        url = openUrl(source)

        begin = time.time()
        dom = parse(url)
        root = dom.documentElement
        person = root.getElementsByTagName("person")
        name = root.getAttribute(r"name")
        name_visited.append(name)
        name_id = author_visited.index(seed)+1
        print name_id
        if not (seed in author_dic):
            author_dic[seed]=[name,name_id]
            name_index.write(name+ u'\r\n')

        node = graph_doc.createElement("node")
        node.setAttribute("id","%s" % author_dic[seed][1])
        node.setAttribute("label", name)
        source_id = author_dic[seed][1]

        inproc_list = root.getElementsByTagName(r"inproceedings")
        proc_list = root.getElementsByTagName(r"proceedings")
        total_list = inproc_list+proc_list
        for inproc in total_list:
            key = inproc.getAttribute(r"key")
            conf_name = key.split('/')[1]
            if conf_name in conf:
                infield_flag = 1
                # if conf_name in conf_dic:
                conf_dic[conf_name] +=1
                # conf.index(conf_name)

        for conf_id in conf_dic:
            node.setAttribute(conf_id,"%s" % conf_dic[conf_id])
        node.setAttribute("infield_flag","%s" % infield_flag)
        graphml.write('\t'+node.toprettyxml())

        if not infield_flag:
            continue

        co_list = root.getElementsByTagName("co")
        for co in co_list:
            na = co.firstChild
            na_addr = na.getAttribute(r"f")
            na_name_node = na.firstChild
            na_name = na_name_node.data
            if not (na_addr in author_visited):
                author_visited.append(na_addr)
                author_queue.append(na_addr)
                na_id = author_visited.index(na_addr) + 1
                author_dic[na_addr] = [na_name,na_id]
                name_index.write(na_name+u'\r\n')
            target_id = author_dic[na_addr][1]
            edge = graph_doc.createElement("edge")
            edge.setAttribute("source","%s" % source_id)
            edge.setAttribute("target","%s" % target_id)
            graphml.write('\t'+edge.toprettyxml())

        end = time.time()
        runtime = end-begin
        if runtime<0.5:
            time.sleep(0.5-runtime)

    graphml.close()
    name_index.close()


Javascript:

function getId(idname) {return document.getElementById(idname);} // with document at first
function getTag(tagname) {return getElementsByTagName(tagname);} // witout document at first


结果:
当我打电话给:

getId("ul-id").innerHTML // this code is working fine 
getId("ul-id").getTag("li").length // not working
document.getElementById("ul-id").getTag("li").length // this also not working

你能告诉我原因以及如何解决这个问题吗? ^ _ ^

4 个答案:

答案 0 :(得分:3)

您无法在getTag上调用getId。 您可以通过执行此类解决方法来解决此问题:

function getTag(context, tagname) {
  return context.getElementsByTagName(tagname);
}

这是fiddle

答案 1 :(得分:3)

如果您真的想在DOM中的元素上添加自定义方法,则需要扩展Element原型以添加方法。

在这种情况下,方法链的工作原理如下:

function getId(idname) {
  return document.getElementById(idname);
}

Element.prototype.getTags = function getNestedElements(tagName) {
  return this.getElementsByTagName(tagName);
};


Element.prototype.getTag = function getNestedElements(tagName) {
  return this.getTag(tagName)[0];
};


document.write(
  getId('ul-id').getTags('li').length
);
<ul id="ul-id">
  <li>Text Content</li>  
  <li>Text Content</li>  
  <li>Text Content</li>  
  <li>Text Content</li>  
</ul>

答案 2 :(得分:2)

试试这个:

var a = document.getElementById('ul-id');
var b = a.getElementsByTagName('li').length;
console.log(b);

答案 3 :(得分:0)

getIdgetTag只是功能。你不能简单地链接它。尝试直接使用DOM apis。

document.getElementById("ul-id").getElementsByTagName("li").length