How to know if there is a link element within the selection

2024/2/27 7:51:24

In Javascript, I'd like determine whether an element, say an A element, exists inside a given range/textRange. The aim is to determine if the user's current selection contains a link. I am building a rich text editor control.

The range object has a commonAncestorContainer (W3C) or parentElement() (Microsoft) method which returns the closest common anscestor of all elements in the range. However, looking inside this element for A elements won't work, since this common ancestor may also contain elements that aren't in the range, since the range can start or end part way through a parent.

How would you achieve this?

Answer

How about selection.containsNode? https://developer.mozilla.org/en/DOM/Selection/containsNode

something like:

var selection = window.getSelection();
var range = selection.getRangeAt(0);
var result = $('a', range.commonAncestorContainer).filter(function() {return selection.containsNode(this);
});
console.log(result);
http://en.ppmy.cn/q/41486.html

Related Q&A

How to use a JS library and a JS function in a Flutter mobile app?

I want to use a JavaScript library in my mobile Flutter project. Here is the link for this library on github. Library How can I use this library and a function(aboutnumber()) from it in my project? Ho…

Uncaught TypeError: Cannot read property offsetTop of null

I am using HTML, CSS and JavaScript to create a web page with a sticky and responsive navigation bar. I created the responsive navigation bar and am trying to make it sticky as well. The issue is that …

Can I assign document.getElementById to variable in javascript [duplicate]

This question already has answers here:Why cant I directly assign document.getElementById to a different function?(5 answers)Closed 6 years ago.I think document.getElementById is a function.So this fu…

nodejs append inside json file in array format

I want the nodejs to append all the data in the JSON format in following format[{"name": "admin","message": "dfd","datetime": "2014-06-03 13:01:39…

IndexedDB and many-to-many Relationships

How are you all handling many-to-many relationships in IndexedDB?For example, say I have a Blog object to hold a blog post and a Tag object for a tag/label of the blog post. One Blog can have many Tag…

how to use status codes 200, 404, 300 ( how jquery done and fail work internally) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 9…

Can I determine if a device is in portrait or landscape mode using jquery?

I want to conditionally alter what the user sees on the photo gallery web site Im going to create based on if the device being used to view the site is in portrait/vertical vs. landscape/horizontal mod…

this bound to subscribe function and not outer component scope in Angular2

I am having an issue in one of my components in Angular2 where as this is bound to wrong context within one of my components. I have other components where this issue is not occurring but I cannot see …

Typescript: Add a function to moment.js namespace

Im trying to add extra functionality to moment.js library. I want to add a function that itself requires a moment() call in its body and Im having a hard time figuring this out.Im using the latest vers…

Display posts on blogger based on Label?

I need to make my homepage to display posts based on a certain label. I dont want all posts to show up at the section of homepage posts. I searched for away on Google Search and I found away that you r…