[英]jQuery: how do I replace part of original hyperlink with non-hyperlinked text?
I have a class, let's say .check-status
, that is a text that has a hyperlink in it. 我有一个类,比方说
.check-status
,它是其中包含超链接的文本。
I have an event listener on click that is supposed to replace that text with a variable statusResponse and then say 'Check again'. 我在单击时有一个事件侦听器,该事件侦听器应将文本替换为变量statusResponse,然后说“再次检查”。 So it would say something like 'Your status is pending.
因此,它会说类似“您的状态为待定。 Check again';
再检查一遍'; but 'Check again' needs to be the original hyperlink and 'Your status is pending' should be regular text.
但是“再次检查”必须是原始超链接,“您的状态为待定”应为常规文本。
I know how to use something like the below: 我知道如何使用以下内容:
$('.check-status').text(statusResponse).append(' Check again')
But that gives me everything as a hyperlink, and I only want the 'Check again' part to be that hyperlink. 但这给了我一切作为超链接的机会,我只希望“再次检查”部分成为该超链接。
How do I achieve that without creating new divs, etc? 如何在不创建新div等的情况下实现这一目标?
Thanks! 谢谢!
As you want the hyperlink to only be on a part of the complete text, you'll need an anchor element only for that text, while the complete text should be formatted with your class. 由于您希望超链接仅出现在全文的一部分上,因此您仅需要该文本的锚元素,而全文应使用您的类设置格式。 So proceed like this:
所以像这样继续:
HTML HTML
<span class="check-status"><a href="status.php" >Your status is pending</a></span>
JavaScript 的JavaScript
var statusResponse = "Your status is pending. ";
var checkAgain = "Check again."
$('.check-status>a').before(statusResponse).text(checkAgain);
var statusResponse = "Your status is pending.", customFlag = false; $(".custom-btn").click(function(){ if(!customFlag){ customFlag = true; var checkStatus = $(".check-status"); checkStatus.before(statusResponse + " ").text("Check again"); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="custom-btn">Click me</button> <br/><br/> <a class="check-status" href="#">Check your status.</a>
So what does this do? 那么,这是做什么的呢?
$(".check-status")
so often it is preferable to have it in a var named checkStatus
. $(".check-status")
因此最好将其包含在名为checkStatus
的var中。 checkStatus
and put it before and outside checkStatus
. checkStatus
和前外把它checkStatus
。 statusResponse
is now placed inside the checkStatus
object. statusResponse
内的文本位于checkStatus
对象内。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.