简体   繁体   English

是否可以在没有onclick的情况下调用JavaScript函数?

[英]JavaScript function callable without onclick?

I am new in a project and we are using thymeleaf combined with JavaScript. 我是一个项目的新手,我们将百里香叶与JavaScript结合使用。

I wrote functions which delivers the background color depending on the availability of the choosen element. 我编写了根据所选元素的可用性提供背景颜色的函数。 I have to copy more or less the functionality of "th:disable" for a div, since this functionality is not available for a div. 我必须或多或少地复制div的“ th:disable”功能,因为该功能不适用于div。 Unfortunately, my functions are never called, I assume the reason is because I have no onClick event or something, I want to call the function it the "th:styleappend". 不幸的是,我的函数从未被调用过,我认为原因是因为我没有onClick事件之类的东西,所以我想将其称为“ th:styleappend”。

<div th:styleappend="|background-color: getBackgroundColor(${element})|">


<script type="text/javascript" th:src="@{/js/gridstack-logic.js}"></script>
    <script th:inline="javascript">


function getBackgroundColor(element){
     console.log("##############in getBackgroundColor");
     if(kachel.bool1&& bool2){
         return rgba(153,168,177,0.05);
     }
     return "${element.backgroundcolor}";
}

</script>

I tried several things but none of them was working 我尝试了几件事,但是都没有用

Please look at the "onLoad" events in the next page: 请查看下一页中的“ onLoad”事件:

onLoad Event onLoad事件

<img src="w3javascript.gif" onload="loadImage()" width="100" height="132">

<script>
function loadImage() {
  alert("Image is loaded");
}
</script>

And also for the changes made on the webpage you could assign onChange event, on eahc of the items that you are trying to verify if changes. 对于在网页上进行的更改,您还可以在要验证是否更改的项目的eahc上分配onChange事件。

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

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