[英]How use jQuery inline?
I have this code:我有这段代码:
<p id = 'drop1' style='color:#008cba; background-color:#fffF00;'
onmouseover="show()" onmouseout="hide()" title="See more">
Hi, my name is text 1.
</p>
that works.这样可行。
But, I'd like to write it in jQuery
.但是,我想把它写在
jQuery
中。 I tried:我试过了:
<p id = 'drop2' style='color:#008cba; background-color:#fffF00;'
$(this).attr('See more')>
Hi, my name is text 2.
</p>
Doesn't work.不起作用。
Is there a way to write a jQuery
only inline form?有没有办法写一个
jQuery
只有内联形式?
You shouldn't be putting code inline with HTML elements in general.一般来说,您不应该将代码与 HTML 元素内联。 It's just as easy to incorporate a script element and a proper event handler.
合并脚本元素和适当的事件处理程序同样容易。 However, I suspect that this is what you're after:
但是,我怀疑这就是您所追求的:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p id="drop2" style="color: #008cba; background-color: #fffF00;" onmouseover="$(this).text('See more')" onmouseout="$(this).text('Hi, my name is text 2')"> Hi, my name is text 2 </p>
The reason your example doesn't work is because jQuery's attr()
method, when passed a single argument, attempts to retrieve the value of that attribute.您的示例不起作用的原因是因为 jQuery 的
attr()
方法在传递单个参数时会尝试检索该属性的值。 There's no such attribute in your HTML.您的 HTML 中没有此类属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.