如何使用单击单选按钮更改链接的多个属性
How to change multiple attributes of a link using onclick of radio button?
我一直在尝试使用单选按钮的onclick属性来更改链接的多个属性。但它不起作用。我是Javascript的新手,所以任何帮助都将不胜感激。到目前为止,这是我的代码。
<input type="radio" name="test" onclick="setAttributes(document.getElementById('submit'), {"class": "first", "href": "http://www.google.com"});">First<br/>
<input type="radio" name="test" onclick="setAttributes(document.getElementById('submit'), {"class": "second", "href": "http://www.stackoverflow.com"});">Second<br/>
<a id="submit" class="" href="">Click Here</a>
<script>
function setAttributes(el, attrs) {
for(var key in attrs) {
el.setAttribute(key, attrs[key]);
}
}
</script>
请参阅此演示
<input type="radio" name="test" onclick="setAttributes(document.getElementById('submit'), {'class': 'first', 'href': 'http://www.google.com'});">First<br/>
<input type="radio" name="test" onclick="setAttributes(document.getElementById('submit'), {'class': 'second', 'href': 'http://www.stackoverflow.com'});">Second<br/>
<a id="submit" class="" href="">Click Here</a>
您传递的参数有"
和'
字符(引号(错误。
JS代码将是
<script>
function setAttributes(el) {
for(var key=1;k<arguments.length;k++) {
el.setAttribute(arguments[k], arguments[k+1]);
k++;
}
}
</script>
HTML将是
<input type="radio" name="test" onclick="setAttributes(document.getElementById('submit'), "class", "first", "href" "http://www.google.com");">First<br/>
只需在onclick="…"中用单个'
更改"
<input type="radio" name="test" onclick="setAttributes(document.getElementById('submit'), {'class': 'first', 'href': 'http://www.google.com'});">First<br/>
<input type="radio" name="test" onclick="setAttributes(document.getElementById('submit'), {'class': 'second', 'href': 'http://www.stackoverflow.com'});">Second<br/>
<a id="submit" class="" href="">Click Here</a>
JAVASCRIPT
function setAttributes(el, attrs) {
for(var key in attrs) {
el.setAttribute(key, attrs[key]);
}
}
DEMO
相关文章:
- 更改跳转到URL操作SSRS 2008 R2的链接目标属性(rc:LinkTarget)
- 如何在不链接/jquery的情况下使用方法应用css属性数组
- 在jQuery中链接时CSS属性不起作用
- 添加数据属性及其值以使用纯javascript进行链接
- 是否有脚本/代码可以一次将链接属性添加到多个链接
- 当图像和链接是 json 对象属性的值时,它们不起作用
- 取消 iframe 中具有 target=“_parent” 属性的链接事件
- 将属性添加到带有href链接但没有#的元素
- 在django帮助文本字段中放置带有onClick属性的链接
- 检查链接对象形式中未定义属性的最佳实践是什么
- 角度:覆盖'@'链接或控制器中隔离作用域中的属性
- 仅将CSS类添加到那些没有“title”属性的链接中
- 如何仅为特定URL向超链接添加属性
- 正在节点d3.js之间创建链接,当前返回Uncaught TypeError:无法读取属性'weight'
- 使用构造函数创建的数字没有其值作为属性,请取消String与构造函数的链接
- 使用jQuery获取链接的属性,并将其作为表单发布到另一个脚本文件
- “data-”属性作为超链接的一部分.HTML 到 JavaScript
- 当锚点的属性“href”包含javascript时,在后台打开链接
- 使用 Javascript 从 Div 中检索 href 属性(链接)
- 我可以将javascript和方法中的属性链接在一起吗?