最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

firefox - How to use a JavaScript variable as a XHTML attributes value? - Stack Overflow

programmeradmin1浏览0评论

I have tried:

<!--...-->
<script type="text/javascript">
  var myVar = "some string";
</script>
<!--...-->
<input name="&{myVar};" ... />
<!--...-->

But using FireFox, the name is set to the literal string: "&{myVar};" instead of the value of myVar...

UDPATE: I see this called JavaScript Entities and hasn't been supported for a long time - but there must be way if one can include JavaScript directly in events (I realize attributes are not events, so JavaScript probably isn't evaluated).. ?! It would be a lot nicer than hardcoding the value as it used elsewehere a lot..

I have tried:

<!--...-->
<script type="text/javascript">
  var myVar = "some string";
</script>
<!--...-->
<input name="&{myVar};" ... />
<!--...-->

But using FireFox, the name is set to the literal string: "&{myVar};" instead of the value of myVar...

UDPATE: I see this called JavaScript Entities and hasn't been supported for a long time - but there must be way if one can include JavaScript directly in events (I realize attributes are not events, so JavaScript probably isn't evaluated).. ?! It would be a lot nicer than hardcoding the value as it used elsewehere a lot..

Share Improve this question edited Aug 7, 2012 at 15:51 Paul Sweatte 24.6k7 gold badges131 silver badges268 bronze badges asked Oct 27, 2010 at 4:13 markmnlmarkmnl 11.3k10 gold badges76 silver badges114 bronze badges 9
  • Not sure what you're trying to do. What you've posted doesn't make any sense. – Geuis Commented Oct 27, 2010 at 4:14
  • I am trying to: "use a JavaScript variable as a XHTML attributes value?".. – markmnl Commented Oct 27, 2010 at 4:15
  • Where did you learn you can insert JavaScript variables in XHTML width &{} syntax? – alex Commented Oct 27, 2010 at 4:18
  • University and here: javascriptkit./javatutors/entity2.shtml – markmnl Commented Oct 27, 2010 at 4:24
  • Lets boil the question down to stock: What goal are you actually trying to achieve? The best way to help you out is to understand exactly what you're attempting to achieve. – Geuis Commented Oct 27, 2010 at 4:37
 |  Show 4 more ments

4 Answers 4

Reset to default 4
var myInput = document.createElement('input');
myInput.setAttribute('name', myVar);

someContainElement.appendChild(myInput);

I'm afraid you have to use Javascript to manipulate the html element. You can use jQuery to make this easier:

<script src="http://code.jquery./jquery-1.4.3.min.js"></script>
<input id="myInput" .../>
<script>
    var myVar = "value";
    $("#myInput").attr("name", myVar);
</script>

Here's what you need to do.

Start by never reading javascript tutorials from a Java site again. Different monsters entirely, plus the tutorials on the site you're referencing are horrible.

Second, get a copy of Javascript the Good Parts, http://www.amazon./dp/0596517742/ This book is perhaps the most useful book ever written (in my opinion) about the language itself. It doesn't say anything about dealing with DOM API's.

I encourage you to learn the language itself before getting too deep into javascript libraries like jQuery. It'll do you a load of good sooner rather than later.

Once you're at least somewhat familiar with the proper ways to use javascript as a language, start investing your time into one library or another. jQuery is probably the most used, well loved, and kindest library out there. It will make dealing with cross-browser crap SO much easier for you.

Both Phil's and Xenoflex's ways, are the better way to go. They are actually doing the same thing just different ways, one using jQuery the other pure Javascript.

Alex's will work as well but then everything is hardcoded and somewhat less flexiable to future changes. The way Alex left his answer open you could print the string or assign it to a variable to be appended to a javascript object.

I think the nearest match to what you're looking for is:

<script type="text/javascript">
  var myVar = "some string";
</script>
...
<script type="text/javascript">
  document.write('<input name="' + myVar + '" ... />')
</script>

But as I said you should really take one of the first two approaches.

发布评论

评论列表(0)

  1. 暂无评论