admin管理员组文章数量:1025782
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
4 Answers
Reset to default 4var 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.
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
4 Answers
Reset to default 4var 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.
本文标签: firefoxHow to use a JavaScript variable as a XHTML attributes valueStack Overflow
版权声明:本文标题:firefox - How to use a JavaScript variable as a XHTML attributes value? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745639139a2160634.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论