简体   繁体   English

使用 javascript/jQuery 单击 span 或 div 类

[英]Clicking a span or div class with javascript/jQuery

I am trying to click the span class "status status-online" or the text within the div class "Online".我正在尝试单击跨度类“状态状态在线”或 div 类“在线”中的文本。 I would like to use jQuery for this, as I am trying to put this in the Chrome Console.我想为此使用 jQuery,因为我试图将其放入 Chrome 控制台。 Here is the HTML for the site:这是该网站的 HTML:

<div class="theme-dark"><div class="popout popout-top-left" style="z-index: 1000; overflow: hidden; visibility: visible; left: 80px; top: 878.533px; height: 210px; width: 217px; transform: translateY(-100%) translateX(0%) translateZ(0px);">
    <div class="status-picker popout-menu">
        <div class="popout-menu-item ">
            <div class="status-icon-text">
                <span class="status status-online" style="margin-right: 14px;"></span>
                <div class="status-text">Online</div></div>
                </div>

Please provide a solution to this in jQuery or Javascript(so I can use tampermonkey).请在 jQuery 或 Javascript 中提供解决方案(以便我可以使用 tampermonkey)。 I have tried to research this but whenever I couldn't find a solution that selects the span, I only found ones that select div IDs and this doesn't have that.我试图对此进行研究,但是每当我找不到选择跨度的解决方案时,我只找到了选择 div ID 的解决方案,而这没有。

Thanks,谢谢,

I have tried:我试过了:

$('.status status-online').click();

But I get this error:但我收到此错误:

Uncaught TypeError: Cannot read property 'click' of null
    at <anonymous>:1:27

you can find any element in page width this code using "jquery":您可以使用“jquery”在此代码中找到页面宽度中的任何元素:

var elem =$(".ClassName");

in your example you should find it with below code:在您的示例中,您应该使用以下代码找到它:

var elem = $(".status-online");

no important what kind of "Html" is ypur element : "span", "Div", "input" or others. ypur 元素是什么类型的“Html”并不重要:“span”、“Div”、“input”或其他。

Using jQuery you can do it with the .click( handler ) function, that Bind an event handler to the "click" JavaScript event, or trigger that event on an element.使用jQuery,您可以使用.click( handler )函数来完成,该函数将事件处理程序绑定到“单击”JavaScript 事件,或在元素上触发该事件。

  • The event handler can be bound to any <span class="status status-online"> :事件处理程序可以绑定到任何<span class="status status-online">

 $( ".status-online" ).click(function() { alert( "Handler for .click() called. on clicking the Span element" ); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <span class="status status-online" style="margin-right: 14px;cursor: pointer;">Click Me i'm a span</span>


The click event is only triggered after this exact series of events:点击事件仅在这一系列事件之后触发:

  • The mouse button is depressed while the pointer is inside the element.当指针在元素内时,鼠标按钮被按下。
  • The mouse button is released while the pointer is inside the element.当指针在元素内时释放鼠标按钮。

This is usually the desired sequence before taking an action.这通常是采取行动之前所需的顺序。 If this is not required, the mousedown or mouseup event may be more suitable.如果这不是必需的,则 mousedown 或 mouseup 事件可能更合适。

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

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