javascript - 可点击的svg功能
问题描述
我有 svg 圆圈,每个下面都有单选按钮,我想根据 svg 点击来控制单选按钮的行为,当我点击第一个 svg 时,它下面的单选按钮被选中,当我点击下一个时,第一个应该取消选中,第二个应该选中,依此类推,这是我迄今为止尝试过的:
$(".circle").click(function(){
$(this).next().prop('checked',true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<svg height="100" width="100">
<circle class="circle" cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
</svg>
<input type="radio" name="check">
<svg height="100" width="100">
<circle class="circle" cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="green" />
</svg>
<input type="radio" name="check">
<svg height="100" width="100">
<circle class="circle" cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="blue" />
</svg>
<input type="radio" name="check">
解决方案
只需name
为具有相同值的每个单选按钮添加属性。
见代码片段:
$("svg").click(function(){
$(this).next().prop('checked', true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<svg height="100" width="100">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
</svg>
<input type="radio" name="rd">
<svg height="100" width="100">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="green" />
</svg>
<input type="radio" name="rd">
<svg height="100" width="100">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="blue" />
</svg>
<input type="radio" name="rd">
推荐阅读
- ethereum - 如何在solidity中解决此错误“如果您发送值,则应支付构造函数。”?
- woocommerce - Woocommerce 在目录/商店页面上显示变体缩略图
- c# - 如何检测图像何时出现在 PictureBox 中
- python - 从“deeppavlov.core.commands.train”导入“train model from_config”时出错
- python - 使用 Flask 返回 Html 属性
- ruby-on-rails - 如何使用下拉列表从独立页面设置 id?或者我如何保存belongs_to、has_many 值?
- javascript - 如何在 vue.js 组件中使用导入的 javascript 库构造函数?
- android - 在 ScrollView 中使用可扩展的 textView 垂直居中 ConstraintLayout(滚动/居中问题)
- android - 在 Firebase Android 中读写
- r - 基于R中多个其他变量中的值有条件地替换变量中的值