注入SVG元素并使用jquery访问它
Inject SVG element and access it using jquery
我正在图形内容类中动态创建svg元素(一个点),并尝试使用jquery访问点元素,但不起作用。有溶胶吗。?
/* code for creating svg element */
$(document).ready(function(){
var x = 0,
$y1 = 0;
$con = $(".graphic-content");
var $svg = $(svg("svg")).attr({
"width": "946", "height": "280"
}).appendTo($con);
/* create a point */
$(function() {
$(svg("circle")).attr({
"cx": x, "cy": $y1,"r":"3","class":"point"
}).appendTo($svg);
});
/* hover effect*/
$("circle").hover(function(){
$(this).css("cursor", "pointer");
alert("Hello!");
});
});
function svg(elem) {
return document.createElementNS("http://www.w3.org/2000/svg", elem);
}
x
和$y1
似乎没有定义?尝试使用附加到svg
的mouseenter
、mouseleave
事件,事件委派。正在删除alert()
调用;使用.toggleClass()
设置cursor:point
/* code for creating svg element */
$(document).ready(function() {
var x = 0,
$y1 = 0;
$con = $(".graphic-content");
var $svg = $(svg("svg")).attr({
"width": "946",
"height": "280"
}).appendTo($con);
/* create a point */
$(function() {
$(svg("circle")).attr({
"cx": x,
"cy": $y1,
"r": "3",
"class": "point"
}).appendTo($svg);
});
/* hover effect*/
$("svg").on("mouseenter", "circle", function(e) {
console.log(e);
$("body").toggleClass("hovered");
// alert("Hello!");
}).on("mouseleave", "circle", function(e) {
console.log(e);
$("body").toggleClass("hovered")
})
});
function svg(elem) {
return document.createElementNS("http://www.w3.org/2000/svg", elem);
}
.hovered {
cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div class="graphic-content"></div>
相关文章:
- 为什么可以't PHP查找可以使用JS或jQuery访问的元素
- 使用jQuery访问动态创建的项
- 使用jQuery访问元素'来自谷歌文档托管的iframe的文本
- 使用jQuery访问Special行中的其他同级
- 如何使用jQuery访问隐藏的Dom元素
- jQuery访问列表视图->ul->李->span->img
- 注入SVG元素并使用jquery访问它
- 使用JQuery访问$.GET中的项目
- 如何使用jQuery访问属于函数内部另一个函数的对象的属性
- 我怎么把范围弄错了?无法从jQuery访问此函数;.在“上”;
- jquery访问多维数组的列
- 如何使用jQuery访问父元素属性
- 通过jQuery访问Sizzle方法
- 使用 jquery 访问 Asp.net 控件(所有选项)
- 使用jquery访问聚合物元素变量
- 使用jQuery访问Flash函数
- jQuery访问node.js全局变量
- 如何使用 jQuery 访问 AJAX 中的 JSON 对象
- 使用 jQuery 访问 Angular 指令中的元素
- jQuery 访问对象