【问题标题】:Wanted script tag elements of child in parent HTML父 HTML 中需要子元素的脚本标记元素
【发布时间】:2016-03-08 17:15:42
【问题描述】:

有 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 中的 Div 加载了xyz.html 的主体,点击abcd.html 中的链接。

【问题讨论】:

  • 您可能希望包含 styleRadio 的定义。
  • 进一步了解您编写的代码以及您认为它应该工作的原因。告诉我们发生了什么,而不是代码正常工作。
  • 你是如何设计收音机的?什么是styleRadio("radio");
  • 我已对您的帖子进行了修改,但以后请花更多时间正确格式化您的代码(确保使用正确的缩进)。还要确保使用正确的标点和语法。作为一个以英语为母语的人,我知道没有什么比阅读没有大写字母的段落更让人分心的了。
  • 看起来函数 styleRadio 完成了所有工作。为了让我们帮助您找出它不起作用的原因,我们需要查看该功能。

标签: javascript jquery html css


【解决方案1】:

假设

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

你需要使用:

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

abcd.html 中包含两个块,因为您的xyz.html 从未用作html,因此永远不会被解释。

【讨论】:

    最近更新 更多