elt.lang ='FR'不工作 [英] elt.lang = 'fr' not working

查看:190
本文介绍了elt.lang ='FR'不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个Firefox扩展的环境中工作

I work in the context of a Firefox extension.

我试图创建一个特定的属性的新元素。我试着在的 elt.lang =ZH =htt​​ps://developer.mozilla.org/en-US/docs/Web/API /element.setAttribute相对=nofollow>如何建立与JavaSript lang属性的HTML元素?好好尝试的工作。元素创建,但没有属性。

I'm trying to create a new element with a specific lang attribute. I tried the method elt.lang = 'zh' given in How to create HTML element with lang attribute in JavaSript? doens't work. The element is created but without the lang attribute.

var wrapper = document.createElement("span");
  var zh = document.createElement("i");
    zh.textContent = symbol;
    zh.lang= "zh-cmn"; // <-- -- -- -- -- -- --
  var srcText = document.createTextNode('blablabla');
wrapper.appendChild(zh);
wrapper.appendChild(srcText);

按预期工作code

然而,当我使用 元素.setAttribute() 方法,它工作正常:

Working as expected code

However when I use the Element.setAttribute() method it works fine:

zh.setAttribute('lang','zh');

为什么 elt.lang /点标记的做法不工作?

Why the elt.lang/dot-notation approach not working ?

推荐答案

的问题涉及到环境(即,扩展)。我是在一个XUL上下文,以便使用 的createElement () 创建一个没有属性的XUL元素。

Issue

The problem was related to the environment (i.e. an extension). I was in a XUL context so using createElement() create a XUL element which don't have a lang attribute.

因此​​,要解决的问题和其他(没有办法选择插入的文本),我不得不强制XHTML命名空间 createElementNS(NS,ELT)

So to fix problem and other (no way to select inserted text), I had to force the XHTML namespace with createElementNS(ns, elt).

var nsXHTML = "http://www.w3.org/1999/xhtml";
var wrapper = document.createElementNS(nsXHTML, "span");
var zh = document.createElementNS(nsXHTML, "i");
zh.lang= "zh-cmn";

这篇关于elt.lang ='FR'不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆