2010-12-06 82 views
0

使用javascript,我希望我的複選框更改輸入文本字段的文本修飾。 所以當它被選中時,輸入文本字段中的文本變成粗體。複選框更改文本裝飾輸入字段

記住我學習,所以我不親,你的傢伙;)

我想是這樣的;

var checkbox = create("input"); 
    checkbox.type = "checkbox"; 
    checkbox.id = "checkboxId" + counter; 
    div.appendChild(checkbox); 
    checkbox.onClick="boldChange(this)" 

var input = create("input"); 
    input.type = "input"; 
    input.id = "inputId" + counter; 
    div.appendChild(input); 



function boldChange() 
var boldgroup = document.getElementsByName(el.name); 
for (var b=0; boldgroup[b]; ++b) 
inputId.style.textDecoration = boldgroup[b].checked ? 'bold' : 'none'; 
} 

我該如何做這項工作? 非常感謝你提前

+0

` text-decoration:bold`是無效的CSS,IIRC – 2010-12-06 17:54:11

回答

2

這是基於你上面的代碼工作的jsfiddle例如:Link to example

代碼片段:(地方下面</body>,使所有的DOM加載)

<script> 
    var div = document.getElementById('div'), 
    counter = 0; 

    var checkbox = document.createElement("input"); 
    checkbox.type = "checkbox"; 
    checkbox.id = "checkboxId" + counter; 
    div.appendChild(checkbox); 
    checkbox.onclick = boldChange; 
    counter++; 

    var input = document.createElement("input"); 
    input.type = "text"; 
    input.id = "inputId" + counter; 
    div.appendChild(input); 



    function boldChange() { 
     input.style.fontWeight = (checkbox.checked)?'bold':'normal'; 
    } 
</script> 
+0

非常感謝你!完美作品 – Opoe 2010-12-06 18:23:35