android-jetpack-compose-button 相关问题


变量改变时变量值的显示不改变(Jetpack Compose Android Studio)

应用程序使用mvvm和jetpack compose,屏幕(View,compose)使用以下代码显示是否已获得权限: val 上下文 = LocalContext.current 通过 remem 访问 val...


Jetpack Compose 记住关键字

我是使用 swiftui 的 iOS 开发人员。 现在我有一个android项目,所以我开始学习jetpack compose。 但我有一个与 swiftui 状态不同的问题。 包 com.example.myapplicati...


无法将修饰符传递到 Jetpack Compose 中的列

我有这个 Composable,它是一个可组合函数,旨在为 Android 开发人员的 Jetpack Compose 课程中的此作业生成所有文本。 @可组合 有趣的文章(修改...


模拟撰写 UI 上的用户交互

我试图了解如何测试用 Jetpack Compose 编写的 Android UI。切换到 Compose 时我的假设是我能够启动应用程序并模拟用户交互,例如......


Jetpack Compose:检查文本最大行数

我有一个场景,如果文本最大行数超过 9,我需要显示一个按钮,否则该按钮不应出现。 我尝试查看 Android 开发者指南,但找不到任何解决方案....


在 Jetpack Compose 中选择 TextField 的所有文本

我正在 Jetpack Compose 中使用 TextField 组件。 如何在获得焦点时选择所有文本?


为什么 BLE 扫描在 Android 13 上对我不起作用?

我正在使用 Jetpack Compose 构建一个扫描 BLE 设备的应用程序。这是一个可组合项,它在进入组合时简单地扫描 BLE 设备,并在退出时停止扫描。我已经...


为什么 Jetpack Compose Material 3 Card 可组合项的内容需要 ColumnScope?

我正在使用 Jetpack Compose,最近注意到 androidx.compose.material3 包中的 Card 可组合项要求其内容在 ColumnScope 中提供。具体来说,


Jetpack 撰写食谱简单应用程序问题

我是 jetpack compose 的新手,习惯使用 XML 来完成各种任务。我正在尝试在 jetpack compose 中构建一个基本的食谱应用程序,但每次运行该应用程序时,都会出现空白的黑屏。我


导航功能上出现 NullPointerException - Jetpack Compose NavHostController

我在 Jetpack Compose 应用程序上使用 FirebaseAuth 实现了 Google OneTap 身份验证。我面临的问题是,在我成功登录后,如果我关闭我的应用程序并重新启动它......


Jetpack Compose 按钮未更新,尽管使用 mutableStateOf 实现

我对 Jetpack Compose 比较陌生,我遇到了一个简单的按钮单击未按预期更新数字的问题。我有一个 Counter 可组合项,它采用初始计数值和 l...


为什么 PopUp 可组合项在 jetpack compose 中不采用圆角?

我在我的jetpack compose项目中使用了PopUp可组合项。我已将角应用到 PopUp 可组合项内的 Surface,因为它本身不采用修饰符参数。但它只剪辑了


如何在 Jetpack compose 中删除 TextField 底线

这是我的代码: 文本域( 值=文本, onValueChange = { onValueChanged(it) }, 修饰符 = 修饰符 .fillMaxWidth() .高度(100.dp) .pa...


StateFlowcollectAsState没有更新他的值jetpack撰写导航

我正在jetpack compose中制作简单的导航应用程序。我正在使用撰写导航更改屏幕,使用一些简单的条件,但我的目标值始终与我初始化的值相同。 ...


为什么我不能使用数据类的字段作为jetpack compose中lazyColumn的item的key?

一个非常非常简单的例子(implementation(platform("androidx.compose:compose-bom:2023.08.00"))): com.study.myapplication 包 导入 android.os.Bundle 导入 androidx.activity。


Jetpack Compose:查找文本在组合之前需要多少行

我试图确定某个文本在合成之前将在屏幕上占据多少行。有办法做到这一点吗?


Android Compose 基础知识 - 项目:创建名片应用程序

在此处输入图像描述当前正在完成此练习 https://developer.android.com/codelabs/basic-android-kotlin-compose-business-card?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%


在测试中更改配置时,Composable 不记得输入

我正在为 Jetpack Compose 组件编写仪器测试。我的可组合项使用 RememberSaveable 来记住配置更改(活动重新启动): @可组合 有趣的添加用户屏幕(){ ...


如何使用 jetpack compose 在 ModalBottomSheet 上放置粘性底部行/栏

将项目放入 ModalBottomSheet 中时,它们会堆叠并展开底部工作表,直到达到一定的高度阈值,超过该高度,用户必须向上滚动底部工作表才能加载...


Jetpack Compose:行中多行文本,具有粗细和换行文本内容

我有一个行布局,里面有两个可组合项: 当内容较长时,文本应包裹自身宽度并占用尽可能多的空间 每次都必须出现在右侧的图像...


如何在jetpack compose中从屏幕向AppBar做出贡献

我想实现一个简单的用户流程,用户可以看到多个屏幕来输入数据。该流程应该共享一个通用的导航栏,每个屏幕在激活时都可以贡献其菜单项...


自 Android Studio 4.1 起,Android 后台 Drawable 无法在按钮中工作

我发现从 Android Studio 4.1 开始,我无法通过在其 android:background 上设置颜色来更改 Button 的背景颜色,只是没有效果。并且自定义 Drawable 也无法正常工作。 我的


Android Compose CircularProgressIndicator 使用最新材料崩溃

这是我在顶级 build.gradle 中使用的 构建脚本{ ext.kotlin_version = '1.9.22' ext.compose_version = '1.5.8' 这是在我的 app/build.gradle 中: //撰写 实施平台('org.


Android Compose:如何显示文本中带有图像标签的 HTML 文本?

我有一个来自 API 的字符串,其中包含 HTML 格式的文本,我需要使用 Compose 将其显示给用户。对于带有格式的简单文本,使用此函数非常简单: @


未解决的参考:lifecycle_version [developer.android.com/codelabs]

代码在这里: 起始代码网址: https://github.com/google-developer-training/basic-android-kotlin-compose-training-dessert-clicker 带有起始代码的分支名称:main Codelab 中的说明:


如何使用jetpack在Exoplayer和media 3中添加自定义ui控制器,就像android中的旧视图系统一样?

我正在将 Exoplayer 与 Media 3 库一起使用。我无法像我们在旧视图系统中那样在 Exoplayer 中添加自定义句柄。 我期望添加自定义 UI,其控制器与旧版本相同......


来自 API 的图像在 iOS 上加载,但在 Android 上不加载(Compose 多平台)

我正在制作我的第一个撰写多平台应用程序并遇到一些问题。 通过 Kamel 和 Moko MVVM 从我的 API 获取一些图像。 在 iOS 上可以,但在 Android 上不行。当然网络权限是...


使用鼠标滚动时 Compose 应用程序崩溃

所以我时不时地在桌面上使用我自己的Android应用程序,每当我在应用程序中的任何位置滚动鼠标时 - 无论是否可滚动,它都会崩溃 java.lang.IllegalStateException:


AnimationSet 未按预期执行顺序动画

如果我手动执行多个连续动画。它按预期工作。这是我的可行代码 扩展.xml 如果我手动执行多个连续动画。它按预期工作。这是我的可行代码 scale_up.xml <?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="1.0" android:fromYScale="1.0" android:toXScale="1.1" android:toYScale="1.1" android:pivotX="50%" android:pivotY="50%" android:fillAfter="true" android:interpolator="@android:anim/decelerate_interpolator" android:duration="@android:integer/config_shortAnimTime" /> scale_down.xml <?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="1.1" android:fromYScale="1.1" android:toXScale="1.0" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:fillAfter="true" android:interpolator="@android:anim/decelerate_interpolator" android:duration="@android:integer/config_shortAnimTime" /> 手动执行连续动画 public void startAnimation(Button button) { // Define the scale up animation Animation scaleUpAnimation = AnimationUtils.loadAnimation(this, R.anim.scale_up); // Define the scale down animation Animation scaleDownAnimation = AnimationUtils.loadAnimation(this, R.anim.scale_down); scaleUpAnimation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { button.startAnimation(scaleDownAnimation); } @Override public void onAnimationRepeat(Animation animation) { } }); button.startAnimation(scaleUpAnimation); } 结果 但是,如果我尝试使用 AnimationSet 替换上述代码,动画结果就会损坏。 public void startAnimation(Button button) { // Define the scale up animation Animation scaleUpAnimation = AnimationUtils.loadAnimation(this, R.anim.scale_up); // Define the scale down animation Animation scaleDownAnimation = AnimationUtils.loadAnimation(this, R.anim.scale_down); // Create an AnimationSet to combine both animations // (It makes no difference whether I am using true or false) AnimationSet animationSet = new AnimationSet(true); animationSet.addAnimation(scaleUpAnimation); animationSet.addAnimation(scaleDownAnimation); // Apply the animation to the button button.startAnimation(animationSet); } 使用AnimationSet的结果(动画不流畅) 我可以知道为什么AnimationSet不起作用吗?谢谢。 我们需要使用setStartOffset来延迟第二个动画的执行。这是解决上述问题的完整代码片段。 public void startAnimation(Button button) { int config_shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime); // Define the scale up animation ScaleAnimation scaleUpAnimation = new ScaleAnimation( 1f, 1.02f, 1f, 1.02f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f ); scaleUpAnimation.setInterpolator(new DecelerateInterpolator()); scaleUpAnimation.setDuration(config_shortAnimTime); scaleUpAnimation.setFillAfter(true); // Define the scale down animation ScaleAnimation scaleDownAnimation = new ScaleAnimation( 1.02f, 1f, 1.02f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f ); scaleDownAnimation.setInterpolator(new AccelerateInterpolator()); scaleDownAnimation.setDuration(config_shortAnimTime); scaleDownAnimation.setFillAfter(true); scaleDownAnimation.setStartOffset(scaleUpAnimation.getDuration()); // Create an AnimationSet to combine both animations AnimationSet animationSet = new AnimationSet(false); animationSet.addAnimation(scaleUpAnimation); animationSet.addAnimation(scaleDownAnimation); // Apply the animation to the button button.startAnimation(animationSet); }


欢迎任何有关如何在 Android 开发中使用 XML 和 Compose 创建以下视图的帮助

如何根据上图做设计代码!!! 这里,当订单阶段为 1 时,订单阶段 2 和 3 的视图显示为灰色,因此根据订单阶段,视图支持...


如何在jetpack compose中使用浮动资源

我的浮点值是360F。我在 res/values 中创建了一个名为 float.xml 的文件。 浮动.xml 我的浮点值是 360F。我在 float.xml 中创建了一个名为 res/values 的文件。 float.xml <?xml version="1.0" encoding="utf-8"?> <resources> <item name="loading_circle_target" format="float" type="dimen">360</item> </resources> 然后像这样使用 @Composable fun LoadingCircle() { val currentRotation by transition.animateValue( 0F, targetValue = dimensionResource(id = R.dimen.loading_circle_target).value, // .. more code in here ) // more code in here } 我在这里遇到错误 android.content.res.Resources$NotFoundException: Resource ID #0x7f070346 type #0x4 is not valid at android.content.res.Resources.getDimension(Resources.java:766) at androidx.compose.ui.res.PrimitiveResources_androidKt.dimensionResource(PrimitiveResources.android.kt:79) 更新 我的最低sdk是21 如果您的目标至少是 API 29,则可以使用: val floatValue = LocalContext.current.resources.getFloat(R.dimen.loading_circle_target) @Composable fun LoadingCircle() { val currentRotation by transition.animateValue( 0F, targetValue = floatValue, // .. more code in here ) // more code in here } 我不得不恢复到旧的 is_phone bool,因为 booleanResource() 支持较旧的 API 级别。所以有 <resources> <bool name="is_phone">false</bool> </resources> 在 values-sw600dp 文件夹中并将其设置为正常值文件夹中的 true。然后就可以像这样使用了 @Composable fun LoadingCircle() { val currentRotation by transition.animateValue( 0F, targetValue = if (booleanResource(id = R.bool.is_phone)) 360f else 180f ) // more code in here }


docker-compose 构建输出到日志文件

我要存档的内容: 我想将 docker 构建过程的输出存储在一个文件中,并在终端中在线查看它。 我尝试的是: docker-compose build --progress plain myenv | docker-compose build --progress plain myenv | docker-compose build --progress plain myenv |给我的...


如何使用javascript forloop在点击时获取html中data-id的值并返回值

这是我的html {% if t_ques %} 今天 {% 表示 t_... 中的项目 这是我的html <ul class="conversations"> {% if t_ques %} <li class="grouping">Today</li> {% for item in t_ques %} <li class="active"> <a id="convers" class="conversation-button text-[#E8F5FC] my-2" href="{% url 'assistant:continuechat' item.pk %}" data-pk="{{item.pk}}"> <i class="fa fa-message fa-regular"></i> {{item.title| truncatewords:04 }} </a> <div class="fade"></div> <div class="edit-buttons"> <button><i class="fa fa-edit"></i></button> <button class="trash" data-id = "{{item.pk}}"><i class="fa fa-trash"></i></button> </div> </li> {% endfor %} {% endif %} {% if y_ques %} <li class="grouping">Yesterday</li> {% for item in y_ques %} <li> <a id="convers" class="conversation-button text-[#E8F5FC] my-2" href="{% url 'assistant:continuechat' item.pk %}" data-pk="{{item.pk}}"> <i class="fa fa-message fa-regular"></i> {{item.title| truncatewords:04 }} </a> <div class="fade"></div> <div class="edit-buttons"> <button><i class="fa fa-edit"></i></button> <button class="trash" data-id = "{{item.pk}}"><i class="fa fa-trash"></i></button> </div> </li> {% endfor %} {% endif %} {% if s_ques %} <li class="grouping">Previous 7 days</li> {% for item in s_ques %} <li> <a id="convers" class="conversation-button text-[#E8F5FC] my-2" href="{% url 'assistant:continuechat' item.pk %}" data-pk="{{item.pk}}"> <i class="fa fa-message fa-regular"></i> {{item.title| truncatewords:04 }} </a> <div class="fade"></div> <div class="edit-buttons"> <button><i class="fa fa-edit"></i></button> <button class="trash" data-id = "{{item.pk}}"><i class="fa fa-trash"></i></button> </div> </li> {% endfor %} {% endif %} {% if more_s_ques %} <li class="grouping">Previous 30 days</li> {% for item in more_s_ques %} <li> <a id="convers" class="conversation-button text-[#E8F5FC] my-2" href="{% url 'assistant:continuechat' item.pk %}" data-pk="{{item.pk}}"> <i class="fa fa-message fa-regular"></i> {{item.title| truncatewords:04 }} </a> <div class="fade"></div> <div class="edit-buttons"> <button><i class="fa fa-edit"></i></button> <button class="trash" data-id = "{{item.pk}}"><i class="fa fa-trash"></i></button> </div> </li> {% endfor %} {% endif %} </ul> </div> 我一直在尝试使用 JavaScript 单击时获取任何 data-pk 的值,但似乎无法实现。这一直说 currentTarget 未定义,当我用 target 替换它时,它说同样的事情。然后,如果我将其替换为 document.querySelector("#convers"),它只会给出第一个值,无论单击哪个 这是我的 JavaScript const conversationButtons = document.querySelectorAll("#convers"); conversationButtons.forEach(button => { button.addEventListener("click", getId); }); function getId(e){ var idValue = e.currentTarget.getAttribute('data-pk'); console.log(idValue); //output corresponding target-id if(!chat_id){ url = `/chat-previous/${idValue}/` }else{ url = 'initiate-chat/' } return url; } $.ajax({ type: 'POST', url: getId(), data: { message: usermsg, chatId: chat_id, // itemId: item_id, csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]').val(), action: 'post' }, success: function(json){ const res = json['chats'] setTimeout(() => { hideTyping(); body.appendChild(mgses(res, "assistant")); scrollToBottomOfResults(); }, 1000); console.log(json) }, error: function(rs, e){ setTimeout(() => { hideTyping(); body.appendChild(setBotResponse("bot")); scrollToBottomOfResults(); }, 600); console.log(rs.error); }, }); 我使用了其他方法,例如 const getId = () => { console.log(document.querySelector(".conversation-button").getAttribute('data-pk')) if(!chat_id){ url = `/chat-previous/${document.querySelector(".conversation-button").getAttribute('data-pk')}/` }else{ url = 'initiate-chat/' } return url; } conversationButtons.forEach(button => { button.addEventListener("click", getId); console.log(button.getAttribute('data-pk')); }); 仍然无法得到我真正想要的。有什么方法可以实现这个目标吗? 委托并使用按钮的类 document.querySelector('.conversations').addEventListener('click', (e) => { let tgt = e.target.closest('.conversation-button'); if (!tgt) return; const idValue = tgt.dataset.pk; console.log(idValue); //output corresponding target-id return chat_id ? 'initiate-chat/' : `/chat-previous/${idValue}/` }) 您做出了多个错误的假设,第一个是您不能有多个具有相同 id 的元素,这将导致您描述的确切行为“它需要第一次出现的 id” 第二件事是你可能想在点击时发出ajax请求,但你构建代码的方式并不能做到这一点。 要解决这些问题,请生成如下 html button.conversation-button button.conversation-button button.conversation-button 然后向所有具有 .conversation-button 类的元素添加一个事件侦听器,并将您的 ajax 调用包装到一个函数中,主要是您已经拥有的函数。 const conversationButtons = document.querySelectorAll(".conversation-button"); conversationButtons.forEach(button => { button.addEventListener("click", doStuff); }); ... function doStuff(e) { $.ajax( ... url: getId(e) ... } function getId(e) { let idValue = e.currentTarget.getAttribute('data-pk'); let url = ...; ... return url; // } 只是一些伪代码让你得到一个想法,祝你好运:)


即使在 Angular 中禁用按钮也能调用方法

我有一个按钮,根据 Angular 中的某些条件被禁用。即使按钮被禁用,我也想启用 onclick 功能。请找到以下代码: 我有一个按钮,根据 Angular 中的某些条件被禁用。即使按钮被禁用,我也想启用 onclick 功能。请找到以下代码: <button *ngIf="overrideSplitByAccountEnabled && (!overrideSplitByaccountListOfAcc && !overrideSplitByAccountGroupValue) || ((isStartTimeInputPresent && !isValidStartTimestamp) || (isEndTimeInputPresent && !isValidEndTimestamp))" type="button" class="btn btn-primary" (click)="validateCustomReRunParameters()" disabled> <i class="glyphicon glyphicon-send" ></i> Send action </button> 即使按钮被禁用,我也想启用单击validateCustomReRunParameters() 我尝试单击,但由于按钮被禁用而无法工作。 您可以将 div 包裹在 button 周围,并将单击事件附加到该 div。如果禁用该按钮,包装器 div 将采用 button 的宽度和高度。当用户点击禁用的 button 时,将触发 div 事件。如果启用 button,div 的宽度和高度将设置为 0。 像这样的东西: <div (click)="validateCustomReRunParameters()"> <button *ngIf="overrideSplitByAccountEnabled && (!overrideSplitByaccountListOfAcc && !overrideSplitByAccountGroupValue) || ((isStartTimeInputPresent && !isValidStartTimestamp) || (isEndTimeInputPresent && !isValidEndTimestamp))" type="button" class="btn btn-primary" (click)="validateCustomReRunParameters()" disabled> <i class="glyphicon glyphicon-send" ></i> Send action </button> </div>


当我在代码中使用 CenterAlignedTopAppBar 时出现编译错误

嗨,我是 jetpack 的新手,这是我的代码 @可组合 有趣的 WoofApp() { 脚手架( 顶部栏 = { CenterAlignedTopAppBar(标题 = { Text(text = "hi") }) // 错误 ...


如何在 Java 测试类中结合使用 compose 和 xml 视图来使用 compose 测试规则

我正在尝试为某些视图编写 UI 测试,其中一些测试已经在 Java 中实现。我还打算包含对 Compose 视图的测试。然而,在我的尝试中,使用任一测试标签......


未提供配置文件:找不到 docker compose up --scale chrome=5

这可能看起来与现有的解决方案类似,但我已经尝试了那里提到的所有解决方案,但似乎没有一个解决方案可以解决我的问题。 我创建了一个 docker compose 文件,Docker-Compose-V3.yml...


SignalR 无法使用 docker compose/container 连接

我在 docker 上建立 SignalR 连接时遇到问题(IIS 运行良好)。 主要目标是运行 docker compose 并将数据从 nodeRed 容器发送到 webApp (.net core 3.1 Blazor) 并通过 v...


Javascript:如何获取单击按钮的innerHTML

构建了一个小费计算器,但希望通过获取正在单击的按钮的innerHTML来缩短代码。 账单总额: 构建了一个小费计算器,但想通过获取正在单击的按钮的innerHTML来缩短代码。 Total Bill: <input id="bill" type="text"> <button type="button" onclick="tip15()">15</button> <button type="button" onclick="tip18()">18</button> <button type="button" onclick="tip20()">20</button> <div id="tip">You owe...</div> function tip15(){ var totalBill = document.getElementById("bill").value; var totalTip = document.onclick.innerHTML var tip = totalBill * 0.15; document.getElementById("tip").innerHTML = "&#36;" +tip } 这种方法的问题是我必须写出三个版本的函数来与小费金额相对应。我想创建一个函数来获取被单击按钮的innerHTML 并在函数中使用该值。我希望它看起来像这样 function tip(){ var totalBill = document.getElementById("bill").value; **var totalTip = GET INNERHTML OF CLICKED BUTTON** var tip = totalBill * ("." + totalTip); document.getElementById("tip").innerHTML = "&#36;" +tip } 这样我就可以在不同的按钮上运行相同的功能。 使用 HTML5 数据属性。 <button type="button" data-tip="15" onclick="getTip(this)">15</button> 您传递给函数的参数this指的是正在单击的按钮。然后你就可以得到这样的属性值: function tip(button){ var tip= button.getAttribute("data-tip"); ... } 剩下的留给你了。 像这样改变:将值传递给tip函数。 <button id="15" type="button" onclick="tip(15)">15</button> <button id="18" type="button" onclick="tip(18)">18</button> <button id="20" type="button" onclick="tip(20)">20</button> function tip(tip_value) { /*use here tip_value as you wish you can use if condition to check the value here*/ var totalBill = document.getElementById("bill").value; var totalTip = tip_value; var tip = totalBill * ("." + totalTip); document.getElementById("tip").innerHTML = "&#36;" +tip; } 将 this.innerHTML 作为参数传递给您的小费函数。 所以你的小费函数应该是这样的: function tip(totalTip) { var totalBill = document.getElementById("bill").value; var tip = totalBill * ("." + totalTip); document.getElementById("tip").innerHTML = "&#36;" +tip } 因此,如果您有一个如下所示的按钮元素: <button type="button" onclick="tip(this.innerHTML)">15</button> 提示函数将被称为 tip(15)。 我会写一个快速解决方案,然后解释为什么我这样做。 建议的解决方案 第 1 部分,HTML <div id="tip-wrapper"> <label for="bill">Total bill:</label> <input name="bill" id="bill" type="text"> <br/> <label for="tipratio">Tip ratio:</label> <button name="tipratio" value="15" type="button">15%</button> <button name="tipratio" value="18" type="button">18%</button> <button name="tipratio" value="20" type="button">20%</button> <div id="final-value">You owe...</div> </div> 第 2 部分,JavaScript var parent = document.getElementById('tip-wrapper'), buttons = parent.getElementsByTagName('button'), max = buttons.length, i; // function that handles stuff function calculate (e) { var bill = document.getElementById('bill'), tipRatio = e.target.value; document.getElementById('final-value').textContent = bill.value * (1+tipRatio/100); } // append event listeners to each button for(i = 0; i < max; i++) { buttons[i].addEventListener('click', calculate, true); } 说明 关于 HTML,并没有“太多”改变。唯一的事情是我使用的东西更符合标准。 我添加了一个 wrapper 元素,这只是为了隔离一些 DOM 遍历,而不是遍历整个文档对象来进行查找(这将加快您的脚本速度)。 您的按钮使用“值”属性,这是最好的。因为您可以以一种方式显示按钮文本,但使用正确的值(请参阅我添加了 % 字符)。 除此之外,我主要添加了适当的标识符和标签。 JavaScript,这是我将更详细地介绍的地方,我将逐步介绍: 您在脚本中要做的第一件事是设置您需要的变量(并获取您将使用的 DOM 元素。这就是我在前 4 行代码中所做的事情。 创建一个通用函数来处理您的计算并更新您的元素,无论其数值如何。我在这里使用的功能是向您的函数添加一个参数 (e),因为 javascript 中的 EVENTS 将 EVENT OBJECT 附加到您的回调函数(在本例中为 calculate();)。 EVENT OBJECT 实际上有很多有用的属性,我使用其中的: target:这是触发事件的元素(即您的按钮之一) 我们所要做的就是获取目标值 (e.target.value) 并将其用于返回最终账单的数学中。 使用addEventListener。人们普遍认为应该将 JavaScript 保留在 HTML 之外,因此不鼓励使用旧的事件方法 (onclick="")。 addEventListener()方法并不太复杂,不做详细介绍,其工作原理如下: htmlObject.addEventListener('事件类型', '回调函数', '冒泡true/false'); 我所做的只是循环遍历所有按钮并附加事件 lsitener。 结束语 使用此脚本,您现在可以添加任何您想要的“按钮”,脚本会将它们全部考虑在内并进行相应调整。只要确保设置它们的“值”即可。 当我写这篇文章时,一些人给出了一些快速的答案。我还不能发表评论(声誉低),所以我会在这里留下我的评论。 一些建议的答案告诉您使用innerHTML来获取小费值。这是错误的,您正在使用表单字段,应该使用 element.value,这就是它的用途。 有些人甚至敢说使用 HTML5 data-* 属性。当然,你可以。但你为什么要这么做呢? HTML 和 DOM 已经提供了完成任务所需的所有工具,而无需使用不必要的属性来污染 HTML。 value="" 属性旨在在表单中使用,它应该在字段值的 data-* 属性上使用。 一般来说,innerHTML旨在获取 HTML,而不一定是文本或值。还有其他方法可以获取您正在寻找的值。对于表单元素,它是 element.value,对于大多数其他 HTML 元素,它是 element.textContent。 希望这些解释有帮助 function tip(o) { var input = document.querySelector("input[id='bill']"); var total = input.value * o.innerHTML; document.querySelector("#tip").innerHTML = "&#36;" + total; } Total Bill: <input id="bill" type="text"> <button type="button" onclick="tip(this)">15</button> <button type="button" onclick="tip(this)">18</button> <button type="button" onclick="tip(this)">20</button> <div id="tip">You owe...</div> 嘿我最近找到了解决这个问题的简单方法: 您可以将内部文本作为元素的 id 提供。在事件处理程序中,您可以通过以下方式访问内部文本: e.目标.id 希望这个解决方案可以帮助您:)


带有 nginx-proxy 的 Docker:不允许附加属性名称

从 ubuntu 18 升级到 Ubuntu 20.04.6 LTS 后 Docker 停止工作 我正在使用 docker-compose 但它完全停止工作。所以我切换到“docker compose” 我现在正在尝试...


MatToolbar:尝试组合不同的工具栏模式

出现以下错误: MatToolbar:尝试组合不同的工具栏模式。显式指定多个 元素或仅将内容放置在 中 出现以下错误: MatToolbar:尝试组合不同的工具栏模式。要么显式指定多个 <mat-toolbar-row> 元素,要么仅将内容放置在单行的 <mat-toolbar> 中。 我的代码已经在 mat-toolbar 中应用了 mat-toolbar-row。然而,该错误仍然存在。 html文件的代码片段如下: <div class="wallpaper"> <mat-toolbar color="primary"> <mat-toolbar-row> <span>Welcome, User</span> <span class="example-fill-remaining-space"></span> <span class="align-center"></span> <span class="example-spacer"></span> <button mat-button>Create Incident </button> <a [routerLink]="['/closed']"><button mat-button style="color: white">Closed Incident</button></a> <span class="example-spacer"></span> <a [routerLink]="['/login']"><button mat-button>Logout</button></a> <img src="../../assets/hsbc_logo3.png" class="logo-hsbc"/> </mat-toolbar-row> <h1>INCIDENT MANAGEMENT SYSTEM</h1> </mat-toolbar> <h1>Welcome to Incident Management System</h1> <mat-card style="background: transparent"> <!-- Title of an Card --> <mat-card-title>Incident Details</mat-card-title> <mat-card-content> <form> <table > <tr> <td> <mat-form-field class="demo-full-width"> <mat-label >Description</mat-label> <textarea [(ngModel)]="incident.description" name="description" cdkTextareaAutosize cdkAutosizeMinRows="1" cdkAutosizeMaxRows="5" matInput></textarea> </mat-form-field> </td> <td> <h4>{{message}}</h4> </td> </tr> <tr> <td> <mat-form-field class="demo-full-width"> <input matInput [matDatepicker]="picker" placeholder="Incident Date" [(ngModel)]="incident.date" name="date" > <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle> <mat-datepicker touchUi="true" #picker></mat-datepicker> </mat-form-field> </td> </tr> <tr> <td colspan="2"> <mat-form-field class="demo-full-width"> <input matInput placeholder="Incident Category" [(ngModel)]="incident.category" name="category"> </mat-form-field> </td> </tr> <tr> <td > <mat-form-field class="demo-full-width"> <mat-select placeholder="Application Owner" [(ngModel)]="incident.owner" name="owner"> <mat-option style="background-color:grey">-- Select--</mat-option> <mat-option style="background-color:cornsilk" value="1">BRV</mat-option> <mat-option style="background-color:cornsilk" value="2">FRTB</mat-option> <mat-option style="background-color:cornsilk" value="3">FSA</mat-option> </mat-select> </mat-form-field> </td> <td> <mat-form-field> <mat-select placeholder="Symphony Group" [(ngModel)]="incident.symphony_group" name="symphony_group"> <mat-option style="background-color:grey">-- Select --</mat-option> <mat-option style="background-color:cornsilk" value="1">MMO SheHacks</mat-option> <mat-option style="background-color: cornsilk" value="2">MMO IT INDIA</mat-option> </mat-select> </mat-form-field> </td> </tr> <tr> <td> <mat-form-field> <mat-select placeholder="Application" [(ngModel)]="incident.application" name="application"> <mat-option style="background-color:grey">-- Select--</mat-option> <mat-option style="background-color:cornsilk" value="1">BRV</mat-option> <mat-option style="background-color:cornsilk" value="2">FRTB</mat-option> <mat-option style="background-color:cornsilk" value="3">FSA</mat-option> </mat-select> </mat-form-field> </td> <td> <mat-form-field> <mat-select placeholder="Status" [(value)]="status" [(ngModel)]="incident.status" name="status"> <mat-option style="background-color:grey">-- Select --</mat-option> <mat-option style="background-color: green" value="1">Available</mat-option> <mat-option style="background-color: orange" value="2">Reduced</mat-option> <mat-option style="background-color: red" value="3">Unavailable</mat-option> </mat-select> </mat-form-field> </td> </tr> <tr> <td colspan="2"> </td> </tr> <tr> <td colspan="2" class="content-center"> <button style="margin:5px" mat-raised-button color="accent" (click)="submit()">Submit Incident</button> <button style="margin:5px" mat-raised-button color="accent" (click)="reset()">Clear</button> <button style="margin:5px" mat-raised-button color="accent">Raise BGCR</button> <button style="margin:5px" mat-raised-button color="accent">Raise Jira</button> </td> </tr> </table> </form> </mat-card-content> </mat-card> </div> 您需要移除 <h1>INCIDENT MANAGEMENT SYSTEM</h1> 或将其放入 <mat-toolbar-row> 内。 我无法在垫子工具栏中填充图像 有什么帮助吗?


导入错误:无法从“jinja2”导入名称“escape”。运行 docker compose up 时出现错误

我分别有以下 Dockerfile 和 docker-compose: Dockerfile: 来自Python:3.9 ENV Python 无缓冲 1 工作目录/应用程序 复制requirements.txt /app/requirements.txt 运行 pip install -r


Dockerfile 无法使用 Prisma 访问 docker-compose postgresql 数据库 url

我真的不知道为什么,但我的 Dockerfile 映像无法访问使用 docker compose 运行的 postgres 数据库 // prisma.schema 生成器客户端{ 提供者=“prisma-client-js” } 数据源...


fastapi、celery、redis、docker compose:无法分配请求的地址

我使用 fastapi、celery、redis 和 docker-compose 来托管 RestAPI。一切都运行良好,除了当我通过 Dockerfile 中的一行代码触发 api 测试时。如果没有这行代码...


Traefik 卡在重启中,不清楚的错误

我正在尝试在 docker compose 中运行 traefik 图像,它之前可以正常工作,但自从我拉取图像后,我从 docker compose log traefik 获得的唯一日志是 command traefik error: field not


在 docker-compose 中 MySQL 连接被拒绝

我在 docker-compose 中从 golang 应用程序连接到 MySQL 时遇到问题。我可以从控制台连接到数据库: mysql -u user -D data -h 0.0.0.0 -P3306 -p 但是,使用 docker-compos 时我无法连接...


docker-compose traefik 与 PathRegexp 的问题

我正在尝试做的事情: 我有一个 docker-compose 文件,其中包含 4 个容器 traefik、UI、API 和 DB。 我想使用 traefik 将容器 UI 和 API 重定向到端口 80,但位于不同的 url 路径上。 ...


使用 kotlin/compose 与 java/xml 指南相比

我猜我应该说我对 kotlin 的 compose 很陌生,我只用过 java/xml 的项目,我在这里很困惑。 使用java,我们为每个屏幕提供具有自己的逻辑/设计的片段。这里...


如何为Android Pay添加假信用卡Visa?

我正在开发一个Android应用程序,它使用android pay进行付款。在 https://codelabs.developers.google.com/codelabs/android-pay/#13 网站中。这是网站上写的


如何在 Kubernetes 中创建像 Docker Compose 中的 Volume 一样的 Volume?

我是 Kubernetes 新手,并尝试在其中创建卷,就像我们在 Docker Compose 中创建一样。 码头工人组成: 版本:'3' 服务: 我的服务: 容器名称:我的容器 图像:我的图像:...


© www.soinside.com 2019 - 2024. All rights reserved.