想要在父HTML中

时间:2016-03-08 04:22:01

标签: javascript jquery html css

有2个html文件:abcd.htmlxyz.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.htmlabcd.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中的链接,xyz.html中的Div加载了abcd.html的正文。

1 个答案:

答案 0 :(得分:0)

假设

function styleRadio(id) {
   $(id).css('opacity','0.5');
}

您需要使用:

<script>
    $(document).ready(function(){
        styleRadio("#radio");
    });
</script>

这两个块都在abcd.html内,因为你的xyz.html从未被用作html,因此从未被解释过。