【发布时间】:2016-03-08 17:15:42
【问题描述】:
有 2 个 html 文件:abcd.html 和 xyz.html。
abcd.html:
<html>
<body>
<div> <!-- src= xyz.html only body of it is taken-->
</body>
</html>
xyz.html:
<html>
<body>
<input type="radio" id="radio">
</body>
</html>
文件:xyz.html,包含一个单选按钮,我需要使用 jQuery 设置它的样式;但是,xyz.html 和 abcd.html 中的以下 jQuery 代码不起作用:
<script>
$(document).ready(function(){
styleRadio("radio");
});
</script>
/* for this function styleRadio, the paramater passed is name parameter of the Radio button
function styleRadio(radioName){
var radioButton = $('input[name="'+ radioName +'"]');
$(radioButton).each(function(){
$(this).wrap( "<span class='custom-radio'></span>" );
if($(this).is(':checked')){
$(this).parent().addClass("selected");
}
});
$(radioButton).click(function(){
if($(this).is(':checked')){
$(this).parent().addClass("selected");
}
$(radioButton).not(this).each(function(){
$(this).parent().removeClass("selected");
});
});
}
css:
.custom-radio{
width: 16px;
height: 16px;
display: inline-block;
position: relative;
z-index: 1;
top: 3px;
background: url("../images/radio.png") no-repeat;
}
.custom-radio:hover{
background: url("../images/radio-hover.png") no-repeat;
}
.custom-radio.selected{
background: url("../images/radio-selected.png") no-repeat;
}
.custom-radio input[type="radio"]{
margin: 1px;
position: absolute;
z-index: 2;
cursor: pointer;
outline: none;
opacity: 0;
/* CSS hacks for older browsers */
_noFocusLine: expression(this.hideFocus=true);
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
filter: alpha(opacity=0);
-khtml-opacity: 0;
-moz-opacity: 0;
}
abcd.html 中的 Div 加载了xyz.html 的主体,点击abcd.html 中的链接。
【问题讨论】:
-
您可能希望包含 styleRadio 的定义。
-
进一步了解您编写的代码以及您认为它应该工作的原因。告诉我们发生了什么,而不是代码正常工作。
-
你是如何设计收音机的?什么是
styleRadio("radio"); -
我已对您的帖子进行了修改,但以后请花更多时间正确格式化您的代码(确保使用正确的缩进)。还要确保使用正确的标点和语法。作为一个以英语为母语的人,我知道没有什么比阅读没有大写字母的段落更让人分心的了。
-
看起来函数
styleRadio完成了所有工作。为了让我们帮助您找出它不起作用的原因,我们需要查看该功能。
标签: javascript jquery html css