简体   繁体   English

在div元素中调用JQuery函数

[英]Call JQuery function in div element

In my html files is invisible element: 在我的html文件中是invisible元素:

<div class="field">
     <div id="warring" style="color:red; display:none;">
        warrning text
     </div>
</div>

and function: 和功能:

function showStepsWarrning(){
  $('warring').show();
}

I need to call this function in if statement from option component, tried to do this by: 我需要在选项组件的if statement调用此函数,并尝试通过以下方式执行此操作:

<option #if(${show.value}) window["showStepsWarrning"]()#end>
  $!project.key.name
</option>

but window["showStepsWarrning"]() doesn't work. 但是window["showStepsWarrning"]()不起作用。

How is it posisble to solve my problem? 解决我的问题有多好?

You have to use # for selecting the element by id. 您必须使用#来通过ID选择元素。 your code shoule be : 您的代码应该是:

function showStepsWarrning(){
    $('#warring').show();
}

I think you are trying to achieve this. 我认为您正在努力实现这一目标。 I am calling showStepsWarrnings() on selection of options and changing the alert colour accordingly. 我在选择选项时调用showStepsWarrnings()并相应地更改警报颜色。 http://codepen.io/shivk/pen/ENqwRP http://codepen.io/shivk/pen/ENqwRP

 function showStepsWarrning1(){ $('#warring').show(); $("#warring").css("color", "red"); } function showStepsWarrning2(){ $('#warring').show(); $("#warring").css("color", "blue"); } function showStepsWarrning3(){ $('#warring').show(); $("#warring").css("color", "green"); } var options = [{"value" : "1", "display" : "One", "fn" : function() { console.log("One clicked");alert("one");} }, {"value" : "2", "display" : "Two", "fn" : function() { console.log($(this).val() + " clicked"); showStepsWarrning1();} }, {"value" : "3", "display" : "Three", "fn" : function() { console.log("Three clicked");showStepsWarrning2(); } }, {"value" : "4", "display" : "Four", "fn" : function() { console.log("Four clicked"); showStepsWarrning3();} }]; var $select = $("select").on("click keyup", function() { var opt = this.options[this.selectedIndex]; $(opt).data("fn").call(opt); }); $.each(options, function(i, val) { $select.append( $("<option/>").attr("value", val.value) .text(val.display) .data("fn", val.fn) ); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> <select></select> <div class="field"> <div id="warring" style="color:red; display:none;">warrning text</div> </div> 

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

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