Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以在SVG标签上使用单击功能?我尝试了元素。单击a<;多边形>;。尝试使用javascript模拟单击。不';行不通_Javascript_Html_Dom_Svg - Fatal编程技术网

是否可以在SVG标签上使用单击功能?我尝试了元素。单击a<;多边形>;。尝试使用javascript模拟单击。不';行不通

是否可以在SVG标签上使用单击功能?我尝试了元素。单击a<;多边形>;。尝试使用javascript模拟单击。不';行不通,javascript,html,dom,svg,Javascript,Html,Dom,Svg,//尝试从下面的HTML中检索HTML元素,并尝试使用下面的Javascript代码单击它 <html> <svg width="300" height="200"> <polygon points="100,10 40,198 190,78 10,78 160,198" style="fill:lime;stroke:purple;stroke-width:5;fill-rule:evenodd;" />

//尝试从下面的HTML中检索HTML元素,并尝试使用下面的Javascript代码单击它

    <html>
        <svg width="300" height="200">
        <polygon points="100,10 40,198 190,78 10,78 160,198"
  style="fill:lime;stroke:purple;stroke-width:5;fill-rule:evenodd;" />
        </svg>
    </html>

    <script>
        var polygons = document.getElementsByTagName('polygon');
        if(polygons.length>0)
        {
            //This statement doesn't work. Trying to simulate the click on the polygon element. The click function doesn't click it
            polygons[0].click();
        }
    </script>

var polygons=document.getElementsByTagName('polygon');
如果(多边形长度>0)
{
//此语句无效。正在尝试模拟对多边形元素的单击。单击函数不会单击它
多边形[0]。单击();
}

是的,这是很有可能的,但您必须首先向多边形添加事件侦听器:

document.querySelector('polygon')。addEventListener('click',()=>console.log('clicked')

与我的同事相反,我将故意且善意地忽略您没有设置单击事件侦听器的事实,而是将回答每个人都没有回答的问题


您尝试使用的
单击方法就是该方法。
此方法可用于从HtmleElement继承的所有元素,但您的
是一个SVGPolygonElement,它不从HtmleElement继承

因此,不,您不能在此元素上使用此方法,因为它的原型链中没有定义此类方法

//一个SVGElement
var poly=document.createElements('http://www.w3.org/2000/svg'多边形';
//装饰物
var unknown=document.createElement('foo');
log(‘(SVG元素)’);
log('has click method:',!!poly.click);
log('从HtmleElement继承:',HtmleElement的多实例);
console.log('//');
log(‘(HTML元素)’);
console.log('has click method:',!!unknown.click');

log('从HtmleElement继承:',HtmleElement的未知实例)单击()应该做什么?单击多边形时是否要运行一些javascript,如果是,请为单击事件添加onclick属性或事件处理程序。感谢您的回复。我正在尝试使用javascript使用DOM click函数模拟对多边形元素的单击。但它没有点击。我尝试使用onclick函数记录日志,但没有创建任何日志。提前谢谢。当你点击一个多边形时会发生什么,你会调用一些javascript函数吗?如果是这样的话,直接调用javascript函数就行了。我正在尝试使用WPT进行一些自动化的性能测试,需要点击它。为此,我正在使用javascript模拟点击。单击时会调用其他函数。再次感谢。手动单击通常会发生什么?直接在WPT中调用这些函数。这并不能回答OPs问题。OP希望以编程方式触发元素上的单击事件。没有抓到一个,谢谢你,凯多。这清楚地回答了我的问题。非常感谢你。