繁体   English   中英

通过Javascript将HTML注入div

[英]Inject HTML into div via Javascript

我试图在由Jira创建的HTML标记中获得onclick / onfocus / onchange。 该项目本身是一个下拉列表,虽然我可以专注于其他ID,但无法在下拉列表中使用

我有的:

<script type ="text/javascript" >
console.log("Testing");
var colorDropDown = document.getElementById('someID');

function changeColor()
{
    //if(value)
    alert("Hello World");
}

document.getElementById("someID").innerHTML ="<onfocus=\"changeColor()\"></select>"
//document.getElementById("customfield_11901").innerHTML = "<select class=\"select cf-select\" name=\"customfield_11901\" id=\"customfield_11901\" onfocus=\"changeColor()\">"

</script>

使用innerHTML之后,onfocus不会出现在页面中。 我还通过复制整个标签并通过HTML输入它来进行尝试。

我在getElementById之后使用了.onchange函数,但这也不起作用。

我会在jQuery下使用.attr()函数:

$('#select_id').attr('onfocus', 'changeColor();');

或者,您可以将addEventListener与纯JS结合使用:

object = document.getElementById('#select_id');
object.addEventListener('focus', 'changeColor();');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM